Skip to content

Commit 9fae0e8

Browse files
fix(server): honor disabled usage hub polling (pingdotgg#10310)
Co-authored-by: Julius Marminge <[email protected]>
1 parent b05ff17 commit 9fae0e8

2 files changed

Lines changed: 116 additions & 5 deletions

File tree

‎apps/server/src/usage/UsageLimitSources.test.ts‎

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
22
import { expect, it } from "@effect/vitest";
3-
import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
3+
import {
4+
ServerConfigStreamEvent,
5+
type ServerSettings as ServerSettingsValue,
6+
UsageLimitSourceId,
7+
} from "@t3tools/contracts";
8+
import * as Duration from "effect/Duration";
49
import * as Effect from "effect/Effect";
510
import * as Layer from "effect/Layer";
11+
import * as PubSub from "effect/PubSub";
12+
import * as Ref from "effect/Ref";
613
import * as Schema from "effect/Schema";
7-
import { HttpClient } from "effect/http";
14+
import * as Stream from "effect/Stream";
15+
import * as TestClock from "effect/testing/TestClock";
16+
import { HttpClient, HttpClientResponse } from "effect/http";
817

918
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
1019
import * as ServerSettings from "../serverSettings.ts";
@@ -83,3 +92,102 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
8392
Effect.scoped,
8493
),
8594
);
95+
96+
it.effect.each([
97+
{ interval: Duration.seconds(0), expectedReads: 1, mode: "disabled" },
98+
{ interval: Duration.minutes(1), expectedReads: 2, mode: "enabled" },
99+
])(
100+
"honors $mode polling while preserving explicit and settings refreshes",
101+
({ interval, expectedReads }) =>
102+
Effect.gen(function* () {
103+
const settings = yield* ServerSettings.ServerSettingsService;
104+
const settingsChanges = yield* PubSub.unbounded<ServerSettingsValue>();
105+
const reads = yield* Ref.make(0);
106+
const allowBackgroundWork = yield* Ref.make(true);
107+
const sources = yield* UsageLimitSources.make.pipe(
108+
Effect.provide([
109+
Layer.succeed(ServerSettings.ServerSettingsService, {
110+
...settings,
111+
streamChanges: Stream.fromPubSub(settingsChanges),
112+
}),
113+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
114+
shouldRunScopeWork: (scope) =>
115+
scope.type === "provider-status"
116+
? Ref.get(allowBackgroundWork)
117+
: Effect.succeed(false),
118+
}),
119+
Layer.succeed(
120+
HttpClient.HttpClient,
121+
HttpClient.make((request) =>
122+
Ref.update(reads, (count) => count + 1).pipe(
123+
Effect.as(HttpClientResponse.fromWeb(request, Response.json({ files: [] }))),
124+
),
125+
),
126+
),
127+
NodeCrypto.layer,
128+
]),
129+
);
130+
yield* sources.streamChanges.pipe(
131+
Stream.filter((snapshots) => snapshots.length === 1),
132+
Stream.runHead,
133+
);
134+
expect((yield* sources.current)[0]?.error).toBeUndefined();
135+
expect(yield* Ref.get(reads)).toBe(1);
136+
137+
yield* TestClock.adjust("1 minute");
138+
expect(yield* Ref.get(reads)).toBe(expectedReads);
139+
140+
yield* sources.refresh;
141+
expect(yield* Ref.get(reads)).toBe(expectedReads + 1);
142+
143+
const current = yield* settings.getSettings;
144+
const updated = yield* settings.updateSettings({
145+
usageLimitSources: {
146+
[UsageLimitSourceId.make("hub")]: {
147+
...current.usageLimitSources[UsageLimitSourceId.make("hub")]!,
148+
label: "Renamed hub",
149+
},
150+
},
151+
});
152+
yield* PubSub.publish(settingsChanges, updated);
153+
yield* sources.streamChanges.pipe(
154+
Stream.filter((snapshots) => snapshots[0]?.label === "Renamed hub"),
155+
Stream.runHead,
156+
);
157+
expect(yield* Ref.get(reads)).toBe(expectedReads + 2);
158+
159+
yield* Ref.set(allowBackgroundWork, false);
160+
yield* TestClock.adjust("1 minute");
161+
expect(yield* Ref.get(reads)).toBe(expectedReads + 2);
162+
163+
const beforeResuming = (yield* sources.current)[0]?.checkedAt;
164+
yield* Ref.set(allowBackgroundWork, true);
165+
yield* settings.updateSettings({ providerHealthRefreshInterval: Duration.minutes(1) });
166+
yield* TestClock.adjust("2 minutes");
167+
expect(yield* Ref.get(reads)).toBeGreaterThan(expectedReads + 2);
168+
yield* sources.streamChanges.pipe(
169+
Stream.filter((snapshots) => snapshots[0]?.checkedAt !== beforeResuming),
170+
Stream.runHead,
171+
);
172+
yield* TestClock.adjust("30 seconds");
173+
const readsBeforeDisabling = yield* Ref.get(reads);
174+
yield* settings.updateSettings({ providerHealthRefreshInterval: Duration.seconds(0) });
175+
yield* TestClock.adjust("30 seconds");
176+
expect(yield* Ref.get(reads)).toBe(readsBeforeDisabling);
177+
}).pipe(
178+
Effect.provide(
179+
ServerSettings.layerTest({
180+
providerHealthRefreshInterval: interval,
181+
usageLimitSources: {
182+
[UsageLimitSourceId.make("hub")]: {
183+
kind: "cliproxy",
184+
url: "https://hub.test",
185+
managementKey: "test-key",
186+
enabled: true,
187+
},
188+
},
189+
}),
190+
),
191+
Effect.scoped,
192+
),
193+
);

‎apps/server/src/usage/UsageLimitSources.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,13 @@ export const make = Effect.gen(function* () {
137137
);
138138
yield* Effect.forever(
139139
interval.pipe(
140-
Effect.flatMap((wait) =>
141-
Effect.sleep(Duration.toMillis(Duration.fromInputUnsafe(wait)) <= 0 ? "60 seconds" : wait),
140+
Effect.flatMap((wait) => Effect.sleep(Duration.isPositive(wait) ? wait : "60 seconds")),
141+
Effect.andThen(interval),
142+
Effect.flatMap((current) =>
143+
Duration.isPositive(current)
144+
? backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })
145+
: Effect.succeed(false),
142146
),
143-
Effect.andThen(backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })),
144147
Effect.flatMap((shouldRun) => (shouldRun ? refresh : Effect.void)),
145148
Effect.ignoreCause({ log: true }),
146149
),

0 commit comments

Comments
 (0)