Bugfix/devexp 765 syncronize token managers#157
Closed
marcos-sinch wants to merge 6 commits into
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Synchronize token managers and refactor HTTP transport
Fixes a race condition in OAuth token creation and renewal under concurrent requests.
Token manager (
token_manager.py)threading.LocktoTokenManagerBase.get_auth_tokenuses double-checked locking so that the initial token fetch happens exactly once even when many threads call it simultaneously.EXPIREDstate +handle_invalid_tokenside-channel with a newrefresh_auth_token(used_token)method: whichever thread holds the lock compares the used (stale) token against the currently cached one and fetches a new token only if they still match, deduplicating concurrent renewal attempts.TokenState.EXPIREDvalue.HTTP transport (
http_transport.py,requests_http_transport.py)prepare_requestandauthenticateout ofsend()and intorequest(), sosend()now receives an already-preparedHttpRequestinstead of a rawHTTPEndpoint. This makessend()a pure I/O primitive and simplifies subclassing._get_bearer_token_from_requestand_set_bearer_tokenhelpers to keep the token-stamping logic in one place.Tests
MockHTTPTransporttest harness with tests that drive the realHTTPTransportRequestsadapter, giving better coverage of the actual I/O path.get_auth_tokenandrefresh_auth_tokenthat assert a single fetch under 20 simultaneous threads.