0

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.

Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53
  • https://doc.akka.io/api/akka/current/akka/stream/scaladsl/FlowOps.html#throttle(cost:Int,per:scala.concurrent.duration.FiniteDuration,maximumBurst:Int,costCalculation:Out=>Int,mode:akka.stream.ThrottleMode):FlowOps.this.Repr[Out] – Viktor Klang Oct 29 '17 at 12:47
  • Thanks a lot, I will give a try – Xiaohe Dong Oct 30 '17 at 12:46
  • Possible duplicate of [How to limit an Akka Stream to execute and send down one message only once per second?](https://stackoverflow.com/questions/36215227/how-to-limit-an-akka-stream-to-execute-and-send-down-one-message-only-once-per-s) – Ramón J Romero y Vigil Oct 30 '17 at 13:12

0 Answers0