Skip to content

Commit dd2f672

Browse files
committed
fix: tighten stream budget accounting
1 parent 66085ec commit dd2f672

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,14 @@ let sessionAffinityWriteVersion = 0;
21702170
continue;
21712171
}
21722172

2173+
if (
2174+
!tryConsumeOutboundRequestAttempt(
2175+
"stream-failover",
2176+
fallbackAccount.index,
2177+
)
2178+
) {
2179+
return null;
2180+
}
21732181
if (
21742182
!accountManager.consumeToken(
21752183
fallbackAccount,
@@ -2179,14 +2187,6 @@ let sessionAffinityWriteVersion = 0;
21792187
) {
21802188
continue;
21812189
}
2182-
if (
2183-
!tryConsumeOutboundRequestAttempt(
2184-
"stream-failover",
2185-
fallbackAccount.index,
2186-
)
2187-
) {
2188-
return null;
2189-
}
21902190
fallbackAccount.accountId = fallbackAccountId;
21912191
if (
21922192
!hadFallbackAccountId &&

lib/request/request-attempt-budget.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@ export function computeOutboundRequestAttemptBudget(params: {
1515
emptyResponseMaxRetries: number;
1616
streamFailoverMax: number;
1717
}): number {
18-
const accountCount = Math.max(1, Math.floor(params.accountCount));
18+
const accountCount = Math.max(
19+
1,
20+
Math.floor(Number.isFinite(params.accountCount) ? params.accountCount : 1),
21+
);
1922
const maxSameAccountRetries = Math.max(
2023
0,
21-
Math.floor(params.maxSameAccountRetries),
24+
Math.floor(
25+
Number.isFinite(params.maxSameAccountRetries)
26+
? params.maxSameAccountRetries
27+
: 0,
28+
),
2229
);
2330
const emptyResponseMaxRetries = Math.max(
2431
0,
25-
Math.floor(params.emptyResponseMaxRetries),
32+
Math.floor(
33+
Number.isFinite(params.emptyResponseMaxRetries)
34+
? params.emptyResponseMaxRetries
35+
: 0,
36+
),
2637
);
2738
const streamFailoverMax = capStreamFailoverMax(params.streamFailoverMax);
2839

test/request-attempt-budget.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ describe("request attempt budget", () => {
2323
).toBe(6);
2424
});
2525

26+
it("sanitizes non-finite retry inputs to deterministic defaults", () => {
27+
expect(
28+
computeOutboundRequestAttemptBudget({
29+
accountCount: Number.NaN,
30+
maxSameAccountRetries: Number.POSITIVE_INFINITY,
31+
emptyResponseMaxRetries: Number.NEGATIVE_INFINITY,
32+
streamFailoverMax: Number.NaN,
33+
}),
34+
).toBe(1);
35+
});
36+
2637
it("keeps the primary stream account plus at most one alternate", () => {
2738
expect(
2839
buildStreamFailoverCandidateOrder(2, [2, 5, 2, 7, 9]),

0 commit comments

Comments
 (0)