我有一个 API 调用另外两个下游 API。一个下游 api ( https://test/foo
) 非常重要,而且速度非常快。另一个慢速下游 api( https://test/bar
) 有其局限性,它的吞吐量每秒只能处理 50 个请求。
我想确保下游 apihttps://test/foo
的优先级高于https://test/bar
. 比如API线程池为75,我只允许50个并行传入连接通过https://test/bar
。其余连接应用于https://test/bar
. 它将https://test/bar
永远不会失败。
我想我应该用 OverflowStrategy.dropNew 应用油门或缓冲https://test/bar
。
这是代码片段。
implicit val actorSystem = ActorSystem("api")
implicit val flowMaterializer = ActorMaterializer()
val httpService = Http()
val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] =
httpService.bind(interface = "0.0.0.0", 3000)
val binding: Future[Http.ServerBinding] =
serverSource
.to(Sink.foreach { connection =>
connection.handleWith(
Flow[HttpRequest]
.map {
case HttpRequest(GET, Uri.Path("/priority-1"), _, _, _) =>
HttpResponse(entity = scala.io.Source.fromURL("https://test/foo").mkString)
case HttpRequest(GET, Uri.Path("/priority-2"), _, _, _) =>
HttpResponse(entity = scala.io.Source.fromURL("https://test/bar").mkString)
}
)
}).run()
问题 1:我应该在哪里throttle(50, 1 seconds, 5000, ThrottleMode.Shaping)
只符合https://test/bar
阈值。
问题 2:如果我想优先处理请求,是否需要应用buffer和OverflowStrategy.dropNew 。https://test/foo
换句话说,https://test/bar
应该删除所有不必要的连接。
问题3:有没有更好的方法来实现这个要求。我connection.handleWith[Flow[HttpRequest, HttpResponse]]
在 Sink 中使用,我不确定这是正确的地方。
如果提供了一些代码片段,那将非常感激并且超级棒:)
提前致谢