[feat][client] PIP-234: Support shared resources in AuthenticationOAuth2 to reduce thread usage #25072
+910
−137
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.
fix: #24795
🔗 Related links
Motivation
Currently, each instance of
AuthenticationOAuth2creates its own HTTP client with dedicated threadpoolsandDNS resolvers. In applications with multiple Pulsar clients or authentication providers, this leads to excessive thread creation and inefficient resource utilization. According to issues #24795 and #24944, there's a need for authentication implementations to share resources like thread pools to reduce system overhead.With this PR, the OAuth2 authentication flow is enhanced as follows:
1.Resource Sharing at Client Initialization:
PulsarClientis created, it builds anAuthenticationInitContextcontaining shared resourcesEventLoopGroup,Timer, andNameResolver2.Authentication Provider Initialization:
Authentication.start(AuthenticationInitContext)method is called with the contextcontext.getService(Class<T>)3.Simplified HTTP Client Creation:
AuthenticationHttpClientFactory.createHttpClient()to obtain pre-configured HTTP clientsAuthenticationInitContextwhen availabletimeouts,SSL certificates) if needed4.Backward-Compatible Factory Methods:
Modifications
1.New API Interface:
Introduced
AuthenticationInitContextinterface providing access to shared servicesAdded
start(AuthenticationInitContext)method to Authentication interface2.Implementation:
Created
AuthenticationInitContextImplimplementing the context interfaceModified
PulsarClientImplto build and pass context during authentication initializationUpdated
AuthenticationOAuth2to use shared HTTP client resources via the context3.HTTP Client Factory:
Created
AuthenticationHttpClientFactoryfor building HTTP clients with shared resourcesAdded
AuthenticationHttpClientConfigfor HTTP client configurationModified OAuth2 flow classes to accept pre-configured HTTP clients and DNS resolvers
4.Factory Updates:
Enhanced
AuthenticationFactoryOAuth2.ClientCredentialsBuilderto support custom HTTP clients and DNS resolversUpdated factory methods to leverage shared resources when available
Verifying this change
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: 3pacccccc#35