extractRequestTimeout

Signature

def extractRequestTimeout: Directive1[Duration] = Directive { inner => ctx

Description

This directive extracts the currently set request timeout.

Warning

Please note that this extracts the request timeout at the current moment, but the timeout can be changed concurrently. See other timeout directives about raciness inherent to timeout directives.

For more information about various timeouts in Akka HTTP see Akka HTTP Timeouts.

Example

Scala
val timeout1 = 500.millis
val timeout2 = 1000.millis
val route =
  path("timeout") {
    withRequestTimeout(timeout1) {
      extractRequestTimeout { t1 =>
        withRequestTimeout(timeout2) {
          extractRequestTimeout { t2 =>
            complete(
              if (t1 == timeout1 && t2 == timeout2) StatusCodes.OK
              else StatusCodes.InternalServerError
            )
          }
        }
      }
    }
  }
Java
Duration timeout1 = Duration.create(500, TimeUnit.MILLISECONDS);
Duration timeout2 = Duration.create(1000, TimeUnit.MILLISECONDS);
Route route =
  path("timeout", () ->
    withRequestTimeout(timeout1, () ->
      extractRequestTimeout( t1 ->
        withRequestTimeout(timeout2, () ->
          extractRequestTimeout( t2 -> {
            if (t1 == timeout1 && t2 == timeout2)
              return complete(StatusCodes.OK);
            else
              return complete(StatusCodes.INTERNAL_SERVER_ERROR);
          })
        )
      )
    )
  );
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.