Hi! I want to ask if it possible to limit by, say, 50 concurrent outbound requests using Akka Http Client API?
I tried with Source.queue[(HttpRequest, Promise[HttpResponse])] and Http().cachedHostConnectionPool but external API which I’m calling return 429 error with retry-after and I don’t want to hit that.
My code
val poolClientFlow
: Flow[(HttpRequest, Promise[HttpResponse]), (Try[HttpResponse], Promise[HttpResponse]), Http.HostConnectionPool] =
Http().cachedHostConnectionPoolHttps[Promise[HttpResponse]](host = host)
val queue: SourceQueueWithComplete[(HttpRequest, Promise[HttpResponse])] =
Source
.queue[(HttpRequest, Promise[HttpResponse])](bufferSize, OverflowStrategy.backpressure)
.throttle(elements = maxNumberOfRequests, per = 1 minute)
.groupedWithin(50,1 second)
.mapConcat(identity)
.via(poolClientFlow)
.to(Sink.foreach({
case ((Success(resp), p)) =>
p.success(resp)
case ((Failure(e), p)) =>
p.failure(e)
}))
def send(request:HttpRequest) = {
val responsePromise = Promise[HttpResponse]()
queue.offer(request -> responsePromise).flatMap{...}
}
I also tried using global limit Actor described here : https://doc.akka.io/docs/akka/current/stream/stream-cookbook.html#globally-limiting-the-rate-of-a-set-of-streams but no success. My idea here was to release token after Future completes.
.throttle()
is not helping too (
Unfortunately I cannot rely on max-open-connections because my server can has multiple clients. And each client should be able to do 50 concurrent requests with Akka Http Client API
Thanks