Search code examples
spring-webfluxproject-reactorspring-webclientreactor-netty

HTTPClient recomendations


I created a webclient customizer that creates a HTTPClient with a custom ConnectionProvider

    @Bean
    WebClientCustomizer webClientCustomizer(CustomizableWebClientProperties properties) {
        return builder -> {
            var connectionProvider = ConnectionProvider
                .builder("customizable-webclient-connection-provider")
                .maxConnections(properties.maxConnections())
                .maxIdleTime(properties.maxIdleTimeout())
                .build();
            var httpClient = HttpClient.create(connectionProvider).compress(true);
            httpClient.warmup().block();
            if (properties.wiretap()) {
                httpClient = httpClient.wiretap("uk.co.something.webclient", LogLevel.DEBUG, TEXTUAL, UTF_8);
            }
            var connector = new ReactorClientHttpConnector(httpClient);
            builder.clientConnector(connector);
        };
    }

And I have several questions:

  • should/could ConnectionProvider instance be shared with HttpClient that are created in that customizer?
  • Is it best to use a HTTPClient per domain/endpoint or could it be shared? There are several services in the app that build their own webclient instance.

Appart from those, any other recommendation is more than welcome!


Solution

  • If the scenario allows, it is always better to share common resources as LoopResource, ConnectionProvider, SslContext etc. By default, if there is no explicit configuration, Reactor Netty will share those.

    The same applies for HttpClient and WebClient. If the scenario allows, it is always better to share HttpClient and WebClient.