helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.44k stars 562 forks source link

LRU cache clientSpiCache in HttpClientRequest.discoverHttpImplementation() is not working as new instances are re-created every request #8930

Closed vasanth-bhat closed 4 days ago

vasanth-bhat commented 6 days ago

Environment Details

This is a huge overhead, especially when HTTPS endpoints are involved. This will result in SSL handshake for each such connections.

It looks like the issue is because the LRU cache “clientSpiCache”. used to cache the protocol version for the endpoints is an instance member and not a static member so we end up creating new empty cache for every HTTP request. https://github.com/helidon-io/helidon/blob/main/webclient/api/src/main/java/io/helidon/webclient/api/HttpClientRequest.java#L35

public class HttpClientRequest extends ClientRequestBase<HttpClientRequest, HttpClientResponse> { …….. private final LruCache<EndpointKey, HttpClientSpi> clientSpiCache = LruCache.create(); private final WebClient webClient;

The clientSpiCache ideally should be static member so that cached content persists across requests. We did try out tests with this change where made this as static member so that cache is reused across requests. , This. significantly improves the performance.

Based on our tests with the sample, we. see about 47% improvement in throughput, changing the clientSpiCache to static member.