【发布时间】:2021-01-07 05:49:29
【问题描述】:
Netty 已弃用 HttpClient#tcpConfiguration。我们正在寻找一种简单的配置方式:
- connectTimeout:等待连接多长时间
- writeTimout:等待写入流的时间(如果在此时间范围内无法传递数据,则会抛出异常)
- readTimeout:等待多长时间从流中读取(如果在此时间范围内没有传递数据,则会抛出异常)
当前代码如下所示:
HttpClient httpClient = HttpClient.create();
Integer connectTimeOutInMs = clientProperties.getConnectTimeOutInMs();
Integer writeTimeOutInMs = clientProperties.getWriteTimeOutInMs();
Integer readTimeout = clientProperties.getReadTimeOutInMs();
httpClient = httpClient.tcpConfiguration(tcpClientParam -> {
TcpClient tcpClient = tcpClientParam;
// Connect timeout configuration
if (connectTimeOutInMs != null) {
tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeOutInMs);
}
return tcpClient.doOnConnected(conn -> {
if (readTimeout != null) {
conn.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS));
}
if (writeTimeOutInMs != null) {
conn.addHandlerLast(new WriteTimeoutHandler(writeTimeOutInMs, TimeUnit.MILLISECONDS));
}
});
});
在不使用 tcpConfiguration 的情况下应该如何配置?以下方法未按预期工作,ReadTimeout 未按预期抛出。
Integer readTimeout = clientProperties.getReadTimeOutInMs();
if (readTimeout != null) {
httpClient.doOnConnected(c -> c.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS)));
}
Integer writeTimeOutInMs = clientProperties.getWriteTimeOutInMs();
if (writeTimeOutInMs != null) {
httpClient.doOnConnected(
c -> c.addHandlerLast(new WriteTimeoutHandler(writeTimeOutInMs, TimeUnit.MILLISECONDS)));
}
Integer connectTimeout = clientProperties.getConnectTimeOutInMs();
if (connectTimeout != null) {
httpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
}
什么是正确的实现?我看到 Netty 提供了HttpClient#responseTimeout(),最后设置了HttpClientOperations#addHandler(NettyPipeline.ResponseTimeoutHandler, new ReadTimeoutHandler(responseTimeout.toMillis(), TimeUnit.MILLISECONDS));。但是没有 connect 和 writeTimeouts 的方法。
【问题讨论】:
标签: netty spring-webflux