如何在reactor-netty中配置池连接空闲超时

Kim*_*mec 6 java reactor-netty

我正在使用具有连接池的 reactor-netty http 客户端(0.7.X 系列),并想配置池连接的空闲超时,但不知道在哪里。

更准确地说,我需要以这样一种方式配置 reactor-netty http 客户端连接池,它会自动关闭在可配置超时内没有看到任何活动的连接。这些连接是打开的,但在某些(可配置的)时间内没有字节传入或传出。

如何配置 reactor-netty http 客户端以抢先关闭空闲连接?

小智 1

通过向通道管道添加 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());
Run Code Online (Sandbox Code Playgroud)