private ReactorClientHttpConnector initConnector() { if (bufferFactory instanceof NettyDataBufferFactory) { ByteBufAllocator allocator = ((NettyDataBufferFactory) bufferFactory).getByteBufAllocator(); return new ReactorClientHttpConnector(this.factory, httpClient -> httpClient.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.ALLOCATOR, allocator))); } else { return new ReactorClientHttpConnector(); } }
private static WebClientProvider getWebClientProvider(ClientConfiguration clientConfiguration) { Duration connectTimeout = clientConfiguration.getConnectTimeout(); Duration soTimeout = clientConfiguration.getSocketTimeout(); TcpClient tcpClient = TcpClient.create(); if (!connectTimeout.isNegative()) { tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(connectTimeout.toMillis())); } if (!soTimeout.isNegative()) { tcpClient = tcpClient.doOnConnected(connection -> connection // .addHandlerLast(new ReadTimeoutHandler(soTimeout.toMillis(), TimeUnit.MILLISECONDS)) .addHandlerLast(new WriteTimeoutHandler(soTimeout.toMillis(), TimeUnit.MILLISECONDS))); } String scheme = "http"; HttpClient httpClient = HttpClient.from(tcpClient); if (clientConfiguration.useSsl()) { httpClient = httpClient.secure(sslConfig -> { Optional<SSLContext> sslContext = clientConfiguration.getSslContext(); sslContext.ifPresent(it -> sslConfig.sslContext(new JdkSslContext(it, true, ClientAuth.NONE))); }); scheme = "https"; } ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient); WebClientProvider provider = WebClientProvider.create(scheme, connector); return provider.withDefaultHeaders(clientConfiguration.getDefaultHeaders()); }
tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, properties.getConnectTimeout());
/** * Creates TcpClient for target address. * * @param address connect address * @return tcp client */ private TcpClient newTcpClient(Address address) { return TcpClient.create(ConnectionProvider.newConnection()) .runOn(loopResources) .host(address.host()) .port(address.port()) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()) .bootstrap(b -> BootstrapHandlers.updateConfiguration(b, "outbound", channelInitializer)); }
/** * Create a {@link ClientHttpConnector} for the given {@link ClientOptions} and * {@link SslConfiguration}. * * @param options must not be {@literal null} * @param sslConfiguration must not be {@literal null} * @return a new {@link ClientHttpConnector}. */ public static ClientHttpConnector create(ClientOptions options, SslConfiguration sslConfiguration) { HttpClient client = HttpClient.create(); if (hasSslConfiguration(sslConfiguration)) { SslContextBuilder sslContextBuilder = SslContextBuilder.forClient(); configureSsl(sslConfiguration, sslContextBuilder); client = client.secure(builder -> { builder.sslContext(sslContextBuilder); }); } client = client.tcpConfiguration(it -> it.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(options.getConnectionTimeout().toMillis()))); return new ReactorClientHttpConnector(client); }
@Test public void connectionWillRetryConnectionAttemptWhenItFailsElastic() throws InterruptedException { connectionWillRetryConnectionAttemptWhenItFails( TcpClient.create() .host("localhost") .port(abortServerPort + 3) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)); }
@Test public void connectionWillRetryConnectionAttemptWhenItFailsFixedChannelPool() throws InterruptedException { connectionWillRetryConnectionAttemptWhenItFails( TcpClient.create(ConnectionProvider.fixed("test", 1)) .host("localhost") .port(abortServerPort + 3) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)); }