-
Notifications
You must be signed in to change notification settings - Fork 36
Fix concurrent RPC fallback skipping healthy endpoints #448
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@ckb-ccc/core": patch | ||
| --- | ||
|
|
||
| fix(core): Fix concurrent client requests skipping healthy RPC fallback | ||
|
|
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,82 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { TransportFallback } from "./fallback.js"; | ||
| import { JsonRpcPayload, Transport } from "./transport.js"; | ||
|
|
||
| const payload: JsonRpcPayload = { | ||
| id: 0, | ||
| jsonrpc: "2.0", | ||
| method: "test", | ||
| params: [], | ||
| }; | ||
|
|
||
| function makeTransport(handler: () => Promise<unknown>): Transport { | ||
| return { request: () => handler() }; | ||
| } | ||
|
|
||
| describe("TransportFallback", () => { | ||
| it("returns result from the first healthy transport", async () => { | ||
| const transport = new TransportFallback([ | ||
| makeTransport(() => Promise.resolve("ok")), | ||
| ]); | ||
| expect(await transport.request(payload)).toBe("ok"); | ||
| }); | ||
|
|
||
| it("falls back to the next transport when the first fails", async () => { | ||
| const transport = new TransportFallback([ | ||
| makeTransport(() => Promise.reject(new Error("fail"))), | ||
| makeTransport(() => Promise.resolve("ok")), | ||
| ]); | ||
| expect(await transport.request(payload)).toBe("ok"); | ||
| }); | ||
|
|
||
| it("throws when all transports fail", async () => { | ||
| const transport = new TransportFallback([ | ||
| makeTransport(() => Promise.reject(new Error("fail A"))), | ||
| makeTransport(() => Promise.reject(new Error("fail B"))), | ||
| ]); | ||
| await expect(transport.request(payload)).rejects.toThrow("fail B"); | ||
| }); | ||
|
|
||
| it("concurrent requests both succeed when the first transport is down", async () => { | ||
| // Transport A is always unavailable; transport B always succeeds. | ||
| // Two concurrent requests should each fall back to B independently. | ||
| const transport = new TransportFallback([ | ||
| makeTransport(() => Promise.reject(new Error("A unavailable"))), | ||
| makeTransport(() => Promise.resolve("ok")), | ||
| ]); | ||
|
|
||
| const results = await Promise.allSettled([ | ||
| transport.request(payload), | ||
| transport.request(payload), | ||
| ]); | ||
|
|
||
| expect(results[0]).toMatchObject({ status: "fulfilled", value: "ok" }); | ||
| expect(results[1]).toMatchObject({ status: "fulfilled", value: "ok" }); | ||
| }); | ||
|
|
||
| it("advances the starting transport after failures so future requests skip known-bad transports", async () => { | ||
| let callsToA = 0; | ||
| let callsToB = 0; | ||
|
|
||
| const transport = new TransportFallback([ | ||
| makeTransport(() => { | ||
| callsToA += 1; | ||
| return Promise.reject(new Error("A unavailable")); | ||
| }), | ||
| makeTransport(() => { | ||
| callsToB += 1; | ||
| return Promise.resolve("ok"); | ||
| }), | ||
| ]); | ||
|
|
||
| // First request: tries A (fails), then B (succeeds) | ||
| await transport.request(payload); | ||
| expect(callsToA).toBe(1); | ||
| expect(callsToB).toBe(1); | ||
|
|
||
| // Second request: should start from B (since A was the last known failure) | ||
| await transport.request(payload); | ||
| expect(callsToA).toBe(1); | ||
| expect(callsToB).toBe(2); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.