Search code examples
sprayakka-http

Spray to Akka HTTP migration: spray.can.client.request-timeout


I'm migrating an application from Spray to Akka HTTP. In the config, I have:

spray {
  can {
    client.request-timeout = infinite
  }
}

What is the equivalent config for Akka HTTP? It appears that request-timeout is now only available on server, and not client.

See https://github.com/akka/akka-http/blob/master/akka-http-core/src/main/resources/reference.conf


Solution

  • From Akka-HTTP docs (http://doc.akka.io/docs/akka-http/current/scala/http/client-side/connection-level.html#timeouts)

    Currently Akka HTTP doesn’t implement client-side request timeout checking itself as this functionality can be regarded as a more general purpose streaming infrastructure feature.

    It should be noted that Akka Streams provide various timeout functionality so any API that uses streams can benefit from the stream stages such as idleTimeout, backpressureTimeout, completionTimeout, initialTimeout and throttle. To learn more about these refer to their documentation in Akka Streams (and Scala Doc).

    Essentially the choice is left to the user to add timeout control to their client-side stream. For instance, in the example shown in the docs you could add a completionTimeout stage to achieve this

      val responseFuture: Future[HttpResponse] =
        Source.single(HttpRequest(uri = "/"))
          .via(connectionFlow)
          .completionTimeout(5.seconds)
          .runWith(Sink.head)
    

    And note that if you're after infinite timeout (as per your Spray config), that will come for free by not adding any timeout stage.