kag0
kag0

Reputation: 6054

How to get the current server request timeout in Akka HTTP

Akka HTTP allows request timeouts to be set from either the global setting in application.conf or modified on a per-route basis with directives.

How can we get the request timeout for the current request and route? Something like

withRequestTimeout(FiniteDuration(5, TimeUnit.SECONDS)) {
  extractRequestTimeout { timeout =>
   complete(s"request would have timed out in $timeout") // request would have timed out in 5 seconds
  }
}

would be perfect.

Upvotes: 1

Views: 1169

Answers (1)

kag0
kag0

Reputation: 6054

This is now possible in Akka HTTP using the extractRequestTimeout directive. PR and docs.

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
            )
          }
        }
      }
    }
  }

Upvotes: 1

Related Questions