fix(usage): cap provider usage fetch timeouts · openclaw/openclaw@7f09d6a
steipete
·
2026-05-30
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,8 @@ import {
|
7 | 7 | parseFiniteNumber, |
8 | 8 | } from "./provider-usage.fetch.shared.js"; |
9 | 9 | |
| 10 | +const MAX_TIMER_TIMEOUT_MS = 2_147_000_000; |
| 11 | + |
10 | 12 | function requireFetchCall( |
11 | 13 | mock: ReturnType<typeof vi.fn>, |
12 | 14 | ): [URL | RequestInfo, RequestInit | undefined] { |
@@ -92,6 +94,19 @@ describe("provider usage fetch shared helpers", () => {
|
92 | 94 | } |
93 | 95 | }); |
94 | 96 | |
| 97 | +it("caps oversized request timeouts before scheduling", async () => { |
| 98 | +const timeoutSpy = vi |
| 99 | +.spyOn(globalThis, "setTimeout") |
| 100 | +.mockReturnValue(1 as unknown as ReturnType<typeof setTimeout>); |
| 101 | +vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined); |
| 102 | +const fetchFnMock = vi.fn(async () => new Response("{}", { status: 200 })); |
| 103 | +const fetchFn = withFetchPreconnect(fetchFnMock); |
| 104 | + |
| 105 | +await fetchJson("https://example.com/usage", {}, MAX_TIMER_TIMEOUT_MS + 1_000_000, fetchFn); |
| 106 | + |
| 107 | +expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS); |
| 108 | +}); |
| 109 | + |
95 | 110 | it("maps configured status codes to token expired", () => { |
96 | 111 | const snapshot = buildUsageHttpErrorSnapshot({ |
97 | 112 | provider: "openai-codex", |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import { resolveTimerTimeoutMs } from "../shared/number-coercion.js"; |
1 | 2 | import { parseFiniteNumber as parseFiniteNumberish } from "./parse-finite-number.js"; |
2 | 3 | import { PROVIDER_LABELS } from "./provider-usage.shared.js"; |
3 | 4 | import type { ProviderUsageSnapshot, UsageProviderId } from "./provider-usage.types.js"; |
@@ -8,8 +9,9 @@ export async function fetchJson(
|
8 | 9 | timeoutMs: number, |
9 | 10 | fetchFn: typeof fetch, |
10 | 11 | ): Promise<Response> { |
| 12 | +const safeTimeoutMs = resolveTimerTimeoutMs(timeoutMs, 1); |
11 | 13 | const controller = new AbortController(); |
12 | | -const timer = setTimeout(controller.abort.bind(controller), timeoutMs); |
| 14 | +const timer = setTimeout(controller.abort.bind(controller), safeTimeoutMs); |
13 | 15 | try { |
14 | 16 | return await fetchFn(url, { ...init, signal: controller.signal }); |
15 | 17 | } finally { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。