scala - 在 Playframework 中忽略 withRequestTimeout
问题描述
当我从 playframework 使用 HTTP 客户端时无法设置超时。这是我的代码:
val request: Future[WSResponse] = WS
.url(url)
.withAuth(user, password, WSAuthScheme.BASIC)
//5 minute timeout in milliseconds
.withRequestTimeout(300000)
.put("")
这不会给我一个错误,但请求将在 2 分钟后直接超时。是否需要设置其他内容才能使用超时?
更新:我使用的是 2.4.8 版本的 playframework。看起来这个版本有以下错误:https ://github.com/playframework/playframework/issues/4846
但是,建议的修补程序不适合我。
val request: Future[WSResponse] = WS
.url(url)
.asInstanceOf[NingWSRequest]
.copy(requestTimeout = Some(-1))
.withAuth(user, password, WSAuthScheme.BASIC)
.withRequestTimeout(-1)
.put("")
两者都会在 2 分钟后给我一个超时。
解决方案
我不能保证这对你有用,但它似乎对我有用,你可能会适应它:
val httpClient = Try(WS.underlying.asInstanceOf[AsyncHttpClient])
.getOrElse(throw new NotImplementedError("Unsupported HTTP client"))
val putBuilder = httpClient.preparePut(url)
putBuilder.setRequestTimeout(-1).addHeader(user, "Basic " + password)
val promise = Promise[Response]()
httpClient.executeRequest(putBuilder.build(),
new AsyncCompletionHandler[Response] {
def onCompleted(response: Response): Response = {
promise.success(response)
response
}
def onThrowable(t: Throwable): Unit = {
promise.failure(t)
super.onThrowable(t)
}
}
)
祝你好运。
推荐阅读
- c - 为什么在嵌入式 c 中输入大小写绝对内存地址?
- pandas - 如何使用正则表达式在熊猫数据框中选择包含特定子字符串的行之后的一行和固定数量的行
- react-native - 如何在 React Native 中使用预先填充的 Realm 数据库?
- html - Index.html 中的 JSON 文件
- android - 以编程方式打开连接的 Wifi 设置 Android 10
- c# - 串口在 Form3 中关闭?
- python - 如果值大于,如何插入行和插值
- rxjs - 订阅导入有什么区别
- php - 如何检查 PHP 中是否绕过了客户端输入验证?
- bash - 命令行中的 API 网关和 Lambda 函数