extractRequestTimeout

Signature

def extractRequestTimeout: Directive1[Duration] 

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 Apache Pekko HTTP see Pekko HTTP Timeouts.

Example

Scala
sourceval 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
sourceDuration 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);
                                        })))));