feat: Add opt-in HttpClient5 connection-pool-manager caching with tenant-aware strategies #1094
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
https://github.com/SAP/cloud-sdk-java-backlog/issues/501
Tasks
This PR introduces a new opt-in API for caching HTTP connection pool managers in the Apache HttpClient 5 module. Connection pool managers can consume significant memory (~100KB each), and this feature allows users to reduce memory consumption by sharing connection managers based on configurable caching strategies.
Some users have reported high memory consumption due to the creation of many connection pool managers. By default, a new connection manager is created for each HTTP client, which provides maximum isolation but can lead to excessive memory usage in multi-tenant applications with many destinations.
This PR provides an opt-in mechanism to cache and reuse connection managers based on various strategies, including a smart
byOnBehalfOf()strategy that automatically determines tenant isolation requirements based on the destination's header providers.Usage Example
New API
ConnectionPoolManagerProviders.noCache()A singleton reference to indicate no caching / no re-use.
This is the default value, the behavior we've had before the change.
ConnectionPoolManagerProviders.cached()A fluent builder API for creating cached connection pool manager providers:
Custom Cache Support
Users can supply their own cache implementation (e.g., Caffeine with expiration):
byOnBehalfOf()StrategyThis strategy intelligently determines whether tenant isolation is required by inspecting the destination's header providers for
IsOnBehalfOfimplementations:NAMED_USER_CURRENT_TENANTorTECHNICAL_USER_CURRENT_TENANT, the connection manager is cached per tenantTECHNICAL_USER_PROVIDERor no custom header provider exist on the destination, the connection manager is shared across tenantsBreaking Changes
None. This is a purely additive change:
noCache()is still the default)@BetaDefinition of Done
Error handling created / updated & covered by the tests aboveDocumentation updated