Skip to content

Commit 2f3b387

Browse files
committed
fix(server): retry a failed provider version lookup on the next probe
1 parent e9298af commit 2f3b387

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎apps/server/src/provider/providerMaintenance.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import * as NodePath from "node:path";
88
import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
99
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
1010
import * as Crypto from "effect/Crypto";
11+
import * as DateTime from "effect/DateTime";
1112
import * as Effect from "effect/Effect";
1213
import * as Sink from "effect/Sink";
1314
import * as Stream from "effect/Stream";
14-
import { HttpClient } from "effect/unstable/http";
15+
import * as TestClock from "effect/testing/TestClock";
16+
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
1517
import { ChildProcessSpawner } from "effect/unstable/process";
1618
import {
1719
createProviderVersionAdvisory,
@@ -148,6 +150,44 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
148150
),
149151
);
150152

153+
it.effect(
154+
"retries a failed registry lookup on the next probe instead of caching it for an hour",
155+
() =>
156+
Effect.gen(function* () {
157+
const cache = new Map<string, { expiresAt: number; version: string | null }>();
158+
const registry = (status: number, body: string) =>
159+
HttpClient.make(() =>
160+
Effect.succeed(
161+
HttpClientResponse.fromWeb(
162+
HttpClientRequest.get("https://registry.npmjs.org/"),
163+
new Response(body, { status }),
164+
),
165+
),
166+
);
167+
const failing = registry(503, "");
168+
const serving = registry(200, '{"version":"2.0.0"}');
169+
const resolve = (client: HttpClient.HttpClient) =>
170+
resolveLatestProviderVersion(manualPackageTool).pipe(
171+
Effect.provideService(ProviderVersionCache, cache),
172+
Effect.provideService(HttpClient.HttpClient, client),
173+
);
174+
175+
expect(yield* resolve(failing)).toBeNull();
176+
const failed = cache.get("@example/package-tool");
177+
expect(failed?.version).toBeNull();
178+
expect(failed!.expiresAt - DateTime.toEpochMillis(yield* DateTime.now)).toBe(60_000);
179+
180+
// Still within the retry window: the failure is served from cache.
181+
expect(yield* resolve(serving)).toBeNull();
182+
yield* TestClock.adjust("61 seconds");
183+
expect(yield* resolve(serving)).toBe("2.0.0");
184+
expect(
185+
cache.get("@example/package-tool")!.expiresAt -
186+
DateTime.toEpochMillis(yield* DateTime.now),
187+
).toBe(60 * 60 * 1_000);
188+
}),
189+
);
190+
151191
it.effect("prefers the installer's own latest version over the npm registry", () =>
152192
resolveLatestProviderVersion({ ...manualPackageTool, latestVersion: "1.2.0" }).pipe(
153193
Effect.provideService(ProviderVersionCache, new Map()),

‎apps/server/src/provider/providerMaintenance.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
2323
import { collectUint8StreamText } from "../stream/collectUint8StreamText.ts";
2424

2525
const LATEST_VERSION_CACHE_TTL_MS = 60 * 60 * 1_000;
26+
// A lookup that timed out or failed retries on the next probe instead of
27+
// silencing the update prompt for the whole cache window.
28+
const LATEST_VERSION_RETRY_TTL_MS = 60 * 1_000;
2629
const LATEST_VERSION_TIMEOUT_MS = 4_000;
2730
const HOMEBREW_INFO_TIMEOUT_MS = 10_000;
2831
const HOMEBREW_INFO_MAX_BYTES = 256 * 1_024;
@@ -703,7 +706,7 @@ export const resolveLatestProviderVersion = Effect.fn("resolveLatestProviderVers
703706

704707
const version = yield* fetchNpmLatestVersion(packageName);
705708
latestVersionCache.set(packageName, {
706-
expiresAt: now + LATEST_VERSION_CACHE_TTL_MS,
709+
expiresAt: now + (version === null ? LATEST_VERSION_RETRY_TTL_MS : LATEST_VERSION_CACHE_TTL_MS),
707710
version,
708711
});
709712
return version;

0 commit comments

Comments
 (0)