-
Notifications
You must be signed in to change notification settings - Fork 0
fix(account-pool): recover from temporary OAuth refresh outages #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
smsunarto
wants to merge
2
commits into
scott/pool-refresh-singleflight
from
scott/pool-refresh-recovery
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| fetchOAuthRefresh, | ||
| TransientOAuthRefreshError, | ||
| } from "./provider-adapter.js"; | ||
|
|
||
| describe("OAuth refresh transport", () => { | ||
| it.each([ | ||
| { status: 400, transient: false }, | ||
| { status: 401, transient: false }, | ||
| { status: 408, transient: true }, | ||
| { status: 429, transient: true }, | ||
| { status: 500, transient: true }, | ||
| { status: 503, transient: true }, | ||
| ])( | ||
| "cancels HTTP $status responses before classifying them", | ||
| async ({ status, transient }) => { | ||
| const cancel = vi.fn(); | ||
| const response = new Response(new ReadableStream({ cancel }), { | ||
| status, | ||
| headers: { "retry-after": "120" }, | ||
| }); | ||
| const refresh = fetchOAuthRefresh( | ||
| { fetch: async () => response, now: () => 1_800_000_000_000 }, | ||
| "https://auth.example/token", | ||
| { refresh_token: "refresh" }, | ||
| ); | ||
| await expect(refresh).rejects.toThrow( | ||
| `OAuth refresh failed with HTTP ${status}.`, | ||
| ); | ||
| if (transient) { | ||
| await expect(refresh).rejects.toBeInstanceOf( | ||
| TransientOAuthRefreshError, | ||
| ); | ||
| await expect(refresh).rejects.toMatchObject({ retryAfterMs: 120_000 }); | ||
| } else { | ||
| await expect(refresh).rejects.not.toBeInstanceOf( | ||
| TransientOAuthRefreshError, | ||
| ); | ||
| } | ||
| expect(cancel).toHaveBeenCalledTimes(1); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(["request", "response"])( | ||
| "classifies a broken %s connection as transient", | ||
| async (stage) => { | ||
| const refresh = fetchOAuthRefresh( | ||
| { | ||
| fetch: async () => { | ||
| if (stage === "request") throw new TypeError("fetch failed"); | ||
| return new Response( | ||
| new ReadableStream({ | ||
| start(controller) { | ||
| controller.error(new TypeError("socket closed")); | ||
| }, | ||
| }), | ||
| ); | ||
| }, | ||
| now: () => 1_800_000_000_000, | ||
| }, | ||
| "https://auth.example/token", | ||
| { refresh_token: "refresh" }, | ||
| ); | ||
| await expect(refresh).rejects.toBeInstanceOf(TransientOAuthRefreshError); | ||
| }, | ||
| ); | ||
|
|
||
| it("times out a pending OAuth request independently of its callers", async () => { | ||
| const controller = new AbortController(); | ||
| const timeout = vi | ||
| .spyOn(AbortSignal, "timeout") | ||
| .mockReturnValue(controller.signal); | ||
| try { | ||
| const refresh = fetchOAuthRefresh( | ||
| { | ||
| fetch: async (_input, init) => { | ||
| const signal = init?.signal; | ||
| if (signal === null || signal === undefined) | ||
| return Response.json({}); | ||
| return new Promise<Response>((_resolve, reject) => { | ||
| signal.addEventListener("abort", () => reject(signal.reason), { | ||
| once: true, | ||
| }); | ||
| }); | ||
| }, | ||
| now: () => 1_800_000_000_000, | ||
| }, | ||
| "https://auth.example/token", | ||
| { refresh_token: "refresh" }, | ||
| ); | ||
| controller.abort( | ||
| new DOMException("OAuth request timed out", "TimeoutError"), | ||
| ); | ||
| await expect(refresh).rejects.toBeInstanceOf(TransientOAuthRefreshError); | ||
| expect(timeout).toHaveBeenCalledWith(15_000); | ||
| } finally { | ||
| timeout.mockRestore(); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: When any selected account hits a transient refresh failure (expired token) during a request, the post-loop
refreshFailurecheck returns 503 even if a later account was actually served and rejected only on quota (429), or the real blocker was another account's terminal error. The returned 503 also carries noretry-afterheader, so the client gets no signal about when the refresh outage clears and may retry immediately or too late. Consider only returning the transient 503 when the request could not be served for an actual refresh reason (no other account with a usable token), and otherwise keep the previous 429/noEligible path.Prompt for AI agents