3

I am using Akka HTTP cachedHostConnectionPoolHttps pool to send requests as part of Akka Streams Flow:

  private val requestFlow: Flow[(HttpRequest, HelperClass), Either[Error, String], _] =
Http().cachedHostConnectionPoolHttps(BaseUrl).mapAsync(1) {
  case (Success(HttpResponse(_, _, entity, _)), _) =>
    Unmarshal(entity).to[String].map(response => {
      Right(response)
    })
  case (Failure(ex), _) =>
    Future(Left(Error(ex)))
}

For some reason not all request responses are being processed. Some results in error:

a.h.i.e.c.PoolGateway - [0 (WaitingForResponseEntitySubscription)] Response entity was not subscribed after 1 second. Make sure to read the response entity body or call `discardBytes()` on it.

How to subscribe to my response while maintaining the flow above?

syforce
  • 141
  • 2
  • 10
  • 1
    Please refer to akka-http doc https://doc.akka.io/docs/akka-http/current/implications-of-streaming-http-entity.html#integrating-with-akka-streams, it suggests a solution for this exact issue. – yiksanchan May 27 '20 at 23:11
  • I read the documentation and updated my solution accordingly, but I still somehow miss the entity – syforce May 28 '20 at 11:01
  • Ok, my problem may come from some multithreaded events happening earlier in the flow. In unit test provided solution worked and errors don't occur anymore – syforce May 28 '20 at 15:07

2 Answers2

3

Although it's not the best solution, you can increase the response subscription timeout like this:

akka.http.host-connection-pool.response-entity-subscription-timeout = 10.seconds

Here is a more thorough discussion: https://github.com/akka/akka-http/issues/1836

Evgeny Mamaev
  • 1,237
  • 1
  • 14
  • 31
1

As suggested in docs, implementing entity handling the following way solves the issue:

          private val requestFlow: Flow[(HttpRequest, HelperClass), Either[Error, String], _] =
Http().cachedHostConnectionPoolHttps(BaseUrl).mapAsync(1) {
  case (Success(HttpResponse(_, _, entity, _)),    _) =>
      entity.dataBytes
        .runReduce(_ ++ _)
        .map(r => Right(r.toString))
  case (Failure(ex), _) =>
    Future(Left(Error(ex)))
}
syforce
  • 141
  • 2
  • 10