Skip to content

Commit 2c566bd

Browse files
committed
fix(server): honor disabled usage hub polling
1 parent 0c200c5 commit 2c566bd

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import { UsageLimitSourceId } from "@t3tools/contracts";
3+
import * as Duration from "effect/Duration";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import * as Ref from "effect/Ref";
7+
import * as Stream from "effect/Stream";
8+
import { TestClock } from "effect/testing";
9+
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
10+
11+
import { BackgroundPolicy } from "../background/BackgroundPolicy.ts";
12+
import { layerTest } from "../serverSettings.ts";
13+
import * as UsageLimitSources from "./UsageLimitSources.ts";
14+
15+
const backgroundPolicy = Layer.succeed(BackgroundPolicy, {
16+
reportClientActivity: () => Effect.void,
17+
removeRpcClient: () => Effect.void,
18+
reportHostPowerState: () => Effect.void,
19+
snapshot: Effect.die("unused"),
20+
streamChanges: Stream.empty,
21+
subscribe: Effect.die("unused"),
22+
hasDemand: () => Effect.succeed(true),
23+
shouldRunScopeWork: () => Effect.succeed(true),
24+
shouldRunOpportunisticWork: Effect.succeed(true),
25+
});
26+
27+
describe("UsageLimitSources refresh interval", () => {
28+
it.effect.each([
29+
{ interval: Duration.seconds(0), expectedReads: 1 },
30+
{ interval: Duration.minutes(1), expectedReads: 2 },
31+
])("polls only when enabled: $expectedReads reads", ({ interval, expectedReads }) =>
32+
Effect.scoped(
33+
Effect.gen(function* () {
34+
const reads = yield* Ref.make(0);
35+
const httpClient = Layer.succeed(
36+
HttpClient.HttpClient,
37+
HttpClient.make((request) =>
38+
Ref.update(reads, (count) => count + 1).pipe(
39+
Effect.as(HttpClientResponse.fromWeb(request, Response.json({ accounts: {} }))),
40+
),
41+
),
42+
);
43+
const sources = yield* UsageLimitSources.make.pipe(
44+
Effect.provide(
45+
Layer.mergeAll(
46+
backgroundPolicy,
47+
httpClient,
48+
layerTest({
49+
providerHealthRefreshInterval: interval,
50+
usageLimitSources: {
51+
[UsageLimitSourceId.make("hub")]: {
52+
kind: "cliproxy",
53+
url: "https://hub.example.com",
54+
managementKey: "test-key",
55+
enabled: true,
56+
},
57+
},
58+
}),
59+
),
60+
),
61+
);
62+
yield* sources.streamChanges.pipe(
63+
Stream.filter((snapshots) => snapshots.length === 1),
64+
Stream.runHead,
65+
);
66+
assert.strictEqual(yield* Ref.get(reads), 1);
67+
68+
yield* TestClock.adjust("61 seconds");
69+
assert.strictEqual(yield* Ref.get(reads), expectedReads);
70+
71+
yield* sources.refresh;
72+
assert.strictEqual(yield* Ref.get(reads), expectedReads + 1);
73+
}),
74+
).pipe(Effect.provide(TestClock.layer())),
75+
);
76+
});

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,16 @@ export const make = Effect.gen(function* () {
172172
yield* Effect.forever(
173173
interval.pipe(
174174
Effect.flatMap((wait) =>
175-
Effect.sleep(Duration.toMillis(Duration.fromInputUnsafe(wait)) <= 0 ? "60 seconds" : wait),
175+
Effect.sleep(
176+
Duration.toMillis(Duration.fromInputUnsafe(wait)) <= 0 ? "60 seconds" : wait,
177+
).pipe(
178+
Effect.andThen(
179+
Duration.toMillis(Duration.fromInputUnsafe(wait)) > 0
180+
? backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })
181+
: Effect.succeed(false),
182+
),
183+
),
176184
),
177-
Effect.andThen(backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })),
178185
Effect.flatMap((shouldRun) => (shouldRun ? refresh : Effect.void)),
179186
Effect.ignoreCause({ log: true }),
180187
),

0 commit comments

Comments
 (0)