Netty has deprecated the usage of HttpClient#tcpConfiguration
. We are looking for a simple way how to configure:
- connectTimeout: How long to wait for a connection
- writeTimout: How long to wait for writing to the stream (if data cannot be delivered within this time frame, exception will be thrown)
- readTimeout: How long to wait to read from the stream (if data is not delivered within this time frame, exception will be thrown)
The current code looks like this:
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));
}
});
});
How should this be configured without use of tcpConfiguration? The following approach did not work as expected and ReadTimeout has not been thrown as expected.
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);
}
What would be the correct implementation? I saw that Netty offers HttpClient#responseTimeout()
, which at the end sets HttpClientOperations#addHandler(NettyPipeline.ResponseTimeoutHandler, new ReadTimeoutHandler(responseTimeout.toMillis(), TimeUnit.MILLISECONDS));
. But there are no methods for connect nor writeTimeouts.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…