我使用的是带有连接池的反应堆-netty http客户端(0.7.X系列),并且希望配置池连接的空闲超时,但不知道在哪里。
更准确地说,我需要配置reactor http客户端连接池,使其能够自动关闭在可配置超时内看不到任何活动的连接。这些连接是打开的,但是在一些(可配置的)时间内,没有任何字节被输入或输出。
如何配置reactory http客户端以先发制人地关闭空闲连接?
发布于 2019-02-20 18:56:26
通过在通道管道中添加netty写和读超时处理程序,我能够在0.7.x分支上完成这一任务。但是,在0.8.x上,这种方法不再起作用。
HttpClient httpClient = HttpClient
.create((HttpClientOptions.Builder builder) -> builder
.host(endpointUrl.getHost())
.port(endpointUrl.getPort())
.poolResources(PoolResources.fixed(connectionPoolName, maxConnections, timeoutPool))
.afterChannelInit(channel -> {
channel.pipeline()
// The write and read timeouts are serving as generic socket idle state handlers.
.addFirst("write_timeout", new WriteTimeoutHandler(timeoutIdle, TimeUnit.MILLISECONDS))
.addFirst("read_timeout", new ReadTimeoutHandler(timeoutIdle, TimeUnit.MILLISECONDS));
})
.build());发布于 2019-07-24 22:14:22
我设法将WebClient (通过底层TcpClient)配置为从反应器中的连接池中删除超时时的空闲连接-netty 0.8.9
我的解决方案部分基于扩展的关于IdleStateHandler的官方文档,以及在创建HttpClient实例时如何正确应用它的研究。
我就是这样做的:
public class IdleCleanupHandler extends ChannelDuplexHandler {
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
final IdleState state = ((IdleStateEvent) evt).state();
if (state == IdleState.ALL_IDLE) { // or READER_IDLE / WRITER_IDLE
// close idling channel
ctx.close();
}
} else {
super.userEventTriggered(ctx, evt);
}
}
}
...
public static WebClient createWebClient(final String baseUrl, final int idleTimeoutSec) {
final TcpClient tcpClient = TcpClient.create(ConnectionProvider.fixed("fixed-pool"))
.bootstrap(bootstrap -> BootstrapHandlers.updateConfiguration(bootstrap, "idleTimeoutConfig",
(connectionObserver, channel) -> {
channel.pipeline()
.addLast("idleStateHandler", new IdleStateHandler(0, 0, idleTimeoutSec))
.addLast("idleCleanupHandler", new IdleCleanupHandler());
}));
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.baseUrl(baseUrl)
.build();
}重要更新:
我的进一步测试表明,在bootstrap钩子期间添加处理程序会破坏池和套接字(通道),Connection不会重用它们。
添加处理程序的正确方法是:
public static WebClient createWebClient(final String baseUrl, final int idleTimeoutSec) {
final TcpClient tcpClient = TcpClient.create(ConnectionProvider.fixed("fixed-pool"))
.doOnConnected(conn -> {
final ChannelPipeline pipeline = conn.channel().pipeline();
if (pipeline.context("idleStateHandler") == null) {
pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 0, idleTimeoutSec))
.addLast("idleCleanupHandler", new IdleCleanupHandler());
}
});
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.baseUrl(baseUrl)
.build();
}注意:在reactor-netty 0.9.x中,将有一种为连接池中的连接配置空闲超时的标准方法,请参见以下提交:https://github.com/reactor/reactor-netty/pull/792
发布于 2020-04-01 20:11:07
在使用TCP客户端的反应堆netty 0.9.x中实现这一目的的最简单方法是使用以下方法,我从@Vladimir-L所引用的链接中获得了这个结果。为您的问题配置"maxIdleTime“。
TcpClient timeoutClient = TcpClient.create(ConnectionProvider.fixed(onnectionPoolName, maxConnections, acquireTimeout,maxIdleTime));https://stackoverflow.com/questions/54761291
复制相似问题