I have one API which calls another Downstream API. The downstream api has its limitation, the throughput of it only can handle 75 request per sec.
I would like to use akka stream to control the API to not exceed 75 requests per sec for the downstream api.
Here is the code snippet.
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)
}
When I sent 100 requests per sec via Gatling. 30% requests failed to send since the downstream api can not handle the load. Is there a way to apply the back pressure to configure like avoiding making downstream api call if it cant be handled.