Repository navigation
fix(auth): refresh the token on INVALID_TOKEN as well as EXPIRED_TOKEN [review/suggestion] - #185
Open
chrishagglund-ship-it wants to merge 6 commits into
Open
chrishagglund-ship-it wants to merge 6 commits into
chrishagglund-ship-it wants to merge 6 commits into
Conversation
After a failover every in-flight request gets a 401 at once. Each one called GetRefreshToken, which always fetches a new token under a global lock, so N requests made N token fetches, one after another. - RefreshToken(settings, client, staleToken) fetches only if the cache still holds the rejected token; otherwise it returns the cached one. - GetToken checks the cache again under the lock, so callers that miss the cache together fetch once. - A 401 on a request sent without a token (such as /token) does not trigger a refresh. The existing RefreshToken and GetRefreshToken overloads are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The retry loop refreshed the token on every 401, including the last attempt, then exited without sending a request with it. A persistent 401 made three token fetches but used only two. Now each refresh is followed by a retry: up to MAX_TOKEN_REFRESH_RETRY_COUNT refreshes, so up to four requests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 401 with a body that is not JSON, such as an HTML page from a proxy or load balancer, made the retry loop throw JsonReaderException from the JSON parse. The caller got a parse error instead of the 401. IsRejectedToken now treats a body it cannot parse as "not a rejected token", so the 401 reaches the caller as ApiException with no refresh. Both the sync and async loops use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop this commit before #184 merges into main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chrishagglund-ship-it
changed the base branch from
main
to
fix/refresh-on-invalid-token
November 20, 2026 16:10
The PR now targets main, so the extra branch filter is not needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chrishagglund-ship-it
changed the base branch from
fix/refresh-on-invalid-token
to
main
November 20, 2026 16:34
chrishagglund-ship-it
marked this pull request as ready for review
November 20, 2026 16:54
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.
Exploring some thoughts on making this 401 token refresh more like proposed improvement in the go-sdk, enhancement to PR #184