2
@Bean
UndertowServletWebServerFactory undertowServletWebServerFactory() {
    UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
    factory.addBuilderCustomizers(
            builder -> {
                builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true)
                        .setWorkerThreads(64)
                        .setIoThreads(25)
                        .setBufferSize(16384)
                        .setServerOption(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH,true);
            });

    return factory;
}




@Bean
public okhttp3.OkHttpClient okHttpClient(@Value("${keypath}") String file,
        @Value("${keypassword}") String password) throws KeyStoreException, IOException,
        CertificateException, NoSuchAlgorithmException, KeyManagementException {

    
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (FileInputStream instream = new FileInputStream(ResourceUtils.getFile(file))) {
        trustStore.load(instream, password.toCharArray());
    } catch (java.security.cert.CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    // X509TrustManager
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    X509TrustManager trustManager = (X509TrustManager) trustManagers[0];

    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(null, new TrustManager[] { trustManager }, null);
    
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    
    Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
    dispatcher.setMaxRequests(maxTotal);
    dispatcher.setMaxRequestsPerHost(maxPerRoute);
    
       OkHttpClient.Builder builder = new OkHttpClient.Builder().
               connectionPool(new ConnectionPool(100,15,TimeUnit.MINUTES)).
               dispatcher(dispatcher).
               protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1));
               builder.sslSocketFactory(sslSocketFactory, trustManager);
               builder.hostnameVerifier(new HostnameVerifier() {
            
          @Override
          public boolean verify(String hostname, SSLSession session) {
            return true;
          }
        });


        OkHttpClient okHttpClient = builder.build();
        
        return okHttpClient; 

}

Also I have given this in the properties file:

server.undertow.worker-threads=64
server.undertow.io-threads=25
  • OkHttp3 version: 4.9.1
  • webserverlet: Undertow

Even though I added the worker-threads and io-threads on the server side either 5 or below 5 connections only getting added. Also the connection pool and dispatcher also not much helpful in this scenario.

I need to achieve more TPS (transactions per second).

AJ_222
  • 21
  • 2
  • 1
    With HTTP/2 you might actually end up with requests on a single socket. So you may want to load balance across multiple client instances that don't share a connection pool. With HTTP/1.1 you should be able to tune as you have done. – Yuri Schimke Feb 16 '21 at 19:42
  • Look at results with https://square.github.io/okhttp/events/ to see if it's reusing connections and how many. – Yuri Schimke Feb 16 '21 at 19:42
  • So there is no way to do multiple connections with http2? – AJ_222 Feb 17 '21 at 17:30
  • Two open feature requests https://github.com/square/okhttp/issues/5950 https://github.com/square/okhttp/issues/4530 – Yuri Schimke Feb 17 '21 at 19:06
  • @YuriSchimke It's not yet developed right? – AJ_222 Feb 18 '21 at 06:36
  • No. You can do externally – Yuri Schimke Feb 18 '21 at 06:46
  • How can I do that? – AJ_222 Feb 21 '21 at 06:36
  • Create multiple OkHttpClient instances and rotate between them – Yuri Schimke Feb 21 '21 at 06:41
  • Can you send me an example which helps me in my case? – AJ_222 Feb 23 '21 at 04:34
  • Sorry, it's not really something I can invest the time into. Your question as stated is not in a form that people can easily help you. https://stackoverflow.com/help/how-to-ask – Yuri Schimke Feb 23 '21 at 15:23
  • 1
    @YuriSchimke Is it possible to print the http2 flow as logs in spring boot? data frame size, window_update, and everything? – AJ_222 Mar 04 '21 at 05:46
  • Yes - see https://stackoverflow.com/a/66398538/1542667 – Yuri Schimke Mar 05 '21 at 08:07
  • @YuriSchimke Thank you so much. – AJ_222 Mar 05 '21 at 23:28
  • @YuriSchimke I have enabled http2 for https using okhttp3 client. Is it possible to add http2 for the http scheme using okhttp3Client? – AJ_222 Mar 18 '21 at 07:18
  • Generally know. HTTP/2 almost always builds on TLS and sends some signals (ALPN) via that protocol. Technically plaintext HTTP/2 with prior knowledge is possible but if you are asking then it makes it unlikely to is supported as it's a specific additional step. – Yuri Schimke Mar 18 '21 at 07:59
  • @YuriSchimke Okay, I will look into it. Thank you – AJ_222 Mar 19 '21 at 06:40
  • @YuriSchimke Is there a way to give initial window size and maximum concurrent streams in okhttp3 client? to make it configurable. – AJ_222 Apr 06 '21 at 12:00
  • I'm pretty certain they just use known defaults (including from spec) https://github.com/square/okhttp/blob/480c20e46bb1745e280e42607bbcc73b2c953d97/okhttp/src/main/kotlin/okhttp3/internal/http2/Settings.kt#L104 and then get updated during the connection in agreement with the server. – Yuri Schimke Apr 06 '21 at 18:53
  • currently, the initial window size is 16777216. I found it using wireshark. – AJ_222 Apr 07 '21 at 06:52
  • why exactly okhttp3 doesn't provide multiple TCP connections with HTTP2 requests? – AJ_222 Apr 30 '21 at 10:01
  • Load balancing is currently an open feature request. – Yuri Schimke Apr 30 '21 at 10:55
  • @YuriSchimke So with okhttp3 round-robin is not possible now? Yet to produce that feature? – AJ_222 May 02 '21 at 08:25
  • @YuriSchimke Is there a way I can do round-robin load balancing with okhttp3 in a proxy? – AJ_222 May 06 '21 at 06:19
  • You can create multiple distinct client instances in your code and load balance between them. But I don't have that as a working example to provide you, and I can't speak to alternative proxy solutions. – Yuri Schimke May 06 '21 at 06:32
  • @YuriSchimke from this proxy to another product that has two IP's, I need to load balance the requests between these two IP's from okhttp3. Is that possible from okhttp3? any developed feature for that? – AJ_222 May 06 '21 at 10:53
  • You can have multiple instances of OkHttp in your client that each has a custom Dns to force a choice of the server when multiple are returned by Dns.SYSTEM. But I can't provide this example, sorry. – Yuri Schimke May 07 '21 at 01:08

0 Answers0