0

我有一个 API 调用另一个下游 API。下游 api 有其局限性,它的吞吐量每秒只能处理 75 个请求。

我想使用 akka 流来控制 API 每秒不超过 75 个下游 api 请求。

这是代码片段。

implicit val actorSystem = ActorSystem("api")
implicit val flowMaterializer = ActorMaterializer()

val httpService = Http()
val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] =
      httpService.bind(interface = "127.0.0.1", 8080)

val binding: Future[Http.ServerBinding] =
      serverSource.to(Sink.foreach { connection =>
        connection.handleWithSyncHandler(requestHandler)
      }).run()

def requestHandler: HttpRequest => HttpResponse = {
    case HttpRequest(GET, Uri.Path("/downstream-api"), _, _, _) =>
      HttpResponse(entity = scala.io.Source.fromURL("https://downstream-api.url/api").mkString)
  }

当我通过 Gatling 每秒发送 100 个请求时。由于下游 api 无法处理负载,30% 的请求发送失败。有没有办法应用背压进行配置,例如如果无法处理,则避免进行下游 api 调用。

4

0 回答 0