如何在Akka HTTP中获取当前服务器请求超时

时间:2018-01-24 22:52:59

标签: scala akka-http

Akka HTTP允许从application.conf中的全局设置设置请求超时,或者使用指令在每个路由的基础上进行修改。

如何获取当前请求和路由的请求超时?像

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

会很完美。

1 个答案:

答案 0 :(得分:0)

现在可以使用extractRequestTimeout指令在Akka HTTP中实现。 PRdocs

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