一次Reactor-Netty连接池错误排查

2020-07-02  本文已影响0人  _小马

关键字

PooledConnectionProvider

前言

在csimple项目中,使用了reactor-netty技术,由于reactor-netty的异步模式,可以很轻松的跟第三方接口完成异步交互,并且对资源消耗极小,配合Dubbo的异步编程,提高性能。

Jar版本

配置代码

@Bean
public WebClient webClient() throws SSLException {
    SslContext sslContext = SslContextBuilder.forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE)
            .build();

    TcpClient tcpClient = TcpClient.create() // 使用默认的配置 ConnectionProvider 
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
            .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(10))
                    .addHandlerLast(new WriteTimeoutHandler(10)));

    HttpClient httpClient = HttpClient.from(tcpClient).secure(t -> t.sslContext(sslContext));
    ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
    return WebClient.builder()
            .clientConnector(connector)
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .build();
}

问题

io.netty.handler.timeout.ReadTimeoutException: null

解决方案

1、可升级Reactor-Netty的版本到 0.9.0-RELEASE 及以上,配置连接的空闲时间。
ConnectionProvider pool = ConnectionProvider.elastic("WebClient Pool", Duration.ofMinutes(1)); // 设置Pool连接的空闲时间为1分钟
TcpClient tcpClient = TcpClient.create(pool);
2、修改不使用连接池的方式,每次请求都新建连接。
TcpClient tcpClient = TcpClient.newConnection(); // 修改TcpClient构建的ConnectorProvider为NewConnectionProvider

扩展阅读

0.9.0版本增加了 PooledRefMetadata 接口表示池的原数据信息,接口提供了一个 idleTime 的方法,用来获取池的空闲时间。方法的注释表明了本次发生的这个问题。

Design notes:
This can be useful to do active idle eviction (eg. some loadbalancers will terminate a TCP connection unilaterally after x minutes).

The evictionPredicate from the PoolConfig can look at this time even in the release phase, because it MUST be reset to 0L before the application of the recycler function and evictionPredicate itself, so it will always look "fresh".

Eviction can happen when an acquire() encounters an available element that is detected as idle.
It could then either:

  • only remove that element and call the allocator
    OR
  • continuously loop until it finds a valid available element, only calling the allocator when it ends up finding no valid element

Another possibility is to use a reaper thread that actively removes idle resources from the available set (but that would need some more synchronization).s

References

1、Spring Webclient Read Timeout after being idle for several minutes

上一篇下一篇

猜你喜欢

热点阅读