PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 11, 2022

[FIXED] How do I set MaxConnPerRoute, ConnectionRequestTimeout, keepAliveStrategy in Spring WebFlux WebClient

 November 11, 2022     httpclient, reactor-netty, spring-rest, spring-webclient, spring-webflux     No comments   

Issue

we have the following custom connection pooling implemented for RestTemplate.

PoolingHttpClientConnectionManager poolingConnManager = 
                new PoolingHttpClientConnectionManager();
        poolingConnManager.setDefaultMaxPerRoute(restClientprops.getRestClientMaxPerRoutePool());
        poolingConnManager.setMaxTotal(restClientprops.getRestClientMaxTotalPool()); 
        HttpClientBuilder httpClientBuilder =  HttpClients.custom()
                .setConnectionManager(poolingConnManager)
                .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
                .setMaxConnPerRoute(restClientprops.getRestClientMaxPerRoutePool())
                .setMaxConnTotal(restClientprops.getRestClientMaxTotalPool()); 
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setConnectTimeout(restClientprops.getConnectTimeout()); 
        requestFactory.setReadTimeout(restClientprops.getReadTimeout()); 
        requestFactory.setConnectionRequestTimeout(restClientprops.getConnectionRequestTimeout()); 
        requestFactory.setHttpClient(httpClientBuilder.build());
        this.restTemplate =  new RestTemplate(requestFactory);

I am changing it to WebClient implementation, and this is what I could come up with.

HttpClient httpClient = HttpClient
                    .create(ConnectionProvider.create("webclient-pool", restClientprops.getRestClientMaxTotalPool()))
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, restClientprops.getConnectTimeout())
                    .responseTimeout(Duration.ofMillis(restClientprops.getConnectionRequestTimeout()))
                    .doOnConnected(conn -> conn.addHandler(new ReadTimeoutHandler(restClientprops.getReadTimeout(), TimeUnit.MILLISECONDS)))
                    .keepAlive(true);
  1. Per this URL https://github.com/reactor/reactor-netty/issues/1159 from what I understood connection request timeout is renamed to responseTimeOut in webclient httpclient. Is that accurate?
  2. How should I set MaxConnPerRoute in webclient that is in the RestTemplate implementation?
  3. Is keepAlive(true) accurate translation of setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)

Appreciate your help.


Solution

  1. Per this URL https://github.com/reactor/reactor-netty/issues/1159 from what I understood connection request timeout is renamed to responseTimeOut in webclient httpclient. Is that accurate?

Yes that's true. More about all timeouts that can be configured for the HttpClient you can find in the Reference Documentation.

How should I set MaxConnPerRoute in webclient that is in the RestTemplate implementation?

You can provide connection pool configuration per remote address (if that's what you mean with MaxConnPerRoute), see javadoc for forRemoteHost.

ConnectionProvider.builder("test")
                  .maxConnections(2) // default max connections
                  .forRemoteHost(<socket-address>,spec -> spec.maxConnections(1)) // max connections only for this socket address
                  .build();

Is keepAlive(true) accurate translation of setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)

If you mean specifying whether the connection is persistent or not then YES this configuration has to be used. By default the connection IS persistent. If you mean SO_KEEPALIVE then NO and you have to use .option() configuration. You can find more in the Reference Documentation.

HttpClient.create()
          .option(ChannelOption.SO_KEEPALIVE, true)

This configuration can be removed is you use the timeout settings provided by Reactor Netty:

.doOnConnected(conn -> conn.addHandler(new ReadTimeoutHandler(restClientprops.getReadTimeout(), TimeUnit.MILLISECONDS)))


Answered By - Violeta Georgieva
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing