Skip to content
Source
Sign inSign up
Open History to see the last commit

UsageLimitSources.ts

200 lines7.6 KB

1import {
2 DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL,
3 UsageLimitSourceError,
4 type UsageLimitSourceConsumeResetCreditInput,
5 type ProviderConsumeResetCreditResult,
6 type ServerSettings,
7 UsageLimitSourceConfig,
8 settingsSecretLiteral,
9 type UsageLimitSourceId,
10 type UsageLimitSourceSnapshot,
11} from "@t3tools/contracts";
12import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
13import * as Context from "effect/Context";
14import * as DateTime from "effect/DateTime";
15import * as Duration from "effect/Duration";
16import * as Effect from "effect/Effect";
17import * as Equal from "effect/Equal";
18import * as Layer from "effect/Layer";
19import * as PubSub from "effect/PubSub";
20import * as Ref from "effect/Ref";
21import * as Semaphore from "effect/Semaphore";
22import * as Stream from "effect/Stream";
23​
24import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
25import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
26import * as SecretResolverLayer from "../secrets/SecretResolver.ts";
27import { resolveSettingsSecrets } from "../secrets/settingsSecretValue.ts";
28import * as Settings from "../serverSettings.ts";
29import { makeCliproxyApi } from "./cliproxyApi.ts";
30​
31export class UsageLimitSources extends Context.Service<
32 UsageLimitSources,
33 {
34 readonly current: Effect.Effect<ReadonlyArray<UsageLimitSourceSnapshot>>;
35 readonly streamChanges: Stream.Stream<ReadonlyArray<UsageLimitSourceSnapshot>>;
36 readonly refresh: Effect.Effect<void>;
37 readonly consumeResetCredit: (
38 input: UsageLimitSourceConsumeResetCreditInput,
39 ) => Effect.Effect<ProviderConsumeResetCreditResult, UsageLimitSourceError>;
40 }
41>()("t3/usage/UsageLimitSources") {}
42​
43function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
44 if (config.label) return config.label;
45 const fallback = id.trim() || "Usage limit source";
46 try {
47 return new URL(config.url).host || fallback;
48 } catch {
49 return fallback;
50 }
52​
53function missingManagementKey(config: UsageLimitSourceConfig): string {
54 return typeof config.managementKey === "string"
55 ? "No management key configured."
56 : "Could not read the management key from its secret provider.";
58​
59/** @public */
60export const make = Effect.gen(function* () {
61 const api = yield* makeCliproxyApi;
62 const settingsService = yield* Settings.ServerSettingsService;
63 const secrets = yield* SecretResolver.SecretResolver;
64 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
65 const readManagementKey = Effect.fn("UsageLimitSources.readManagementKey")(function* (
66 config: UsageLimitSourceConfig,
67 ) {
68 const resolved = yield* resolveSettingsSecrets(secrets, UsageLimitSourceConfig, config);
69 return settingsSecretLiteral(resolved.managementKey);
70 });
71 const stateRef = yield* Ref.make<ReadonlyArray<UsageLimitSourceSnapshot>>([]);
72 const changes = yield* Effect.acquireRelease(
73 PubSub.unbounded<ReadonlyArray<UsageLimitSourceSnapshot>>(),
74 PubSub.shutdown,
75 );
76​
77 const readSource = Effect.fn("UsageLimitSources.readSource")(function* (
78 id: UsageLimitSourceId,
79 config: UsageLimitSourceConfig,
80 ) {
81 const checkedAt = DateTime.formatIso(yield* DateTime.now);
82 const base = { id, kind: config.kind, label: sourceLabel(id, config), checkedAt } as const;
83 const managementKey = yield* readManagementKey(config);
84 if (managementKey.length === 0) {
85 return { ...base, accounts: [], error: missingManagementKey(config) };
86 }
87 const accounts = yield* api
88 .readAccounts({ url: config.url, managementKey })
89 .pipe(Effect.result);
90 if (accounts._tag === "Failure") {
91 yield* Effect.logDebug("usage limit source read failed", { id, cause: accounts.failure });
92 return { ...base, accounts: [], error: accounts.failure.detail };
93 }
94 return { ...base, accounts: accounts.success };
95 });
96​
97 const publish = Effect.fn("UsageLimitSources.publish")(function* (
98 next: ReadonlyArray<UsageLimitSourceSnapshot>,
99 ) {
100 const changed = yield* Ref.modify(stateRef, (previous) =>
101 Equal.equals(previous, next) ? [false, previous] : [true, next],
102 );
103 if (changed) yield* PubSub.publish(changes, next);
104 });
105​
106 // Serialize refreshes so slow old-settings reads cannot resurrect removed sources after newer refreshes.
107 const refreshLock = yield* Semaphore.make(1);
108 const refresh = Effect.gen(function* () {
109 const settings = yield* settingsService.getSettings.pipe(
110 Effect.orElseSucceed((): ServerSettings | null => null),
111 );
112 const entries = Object.entries(settings?.usageLimitSources ?? {}).filter(
113 ([, config]) => config.enabled,
114 );
115 const snapshots = yield* Effect.forEach(
116 entries,
117 ([id, config]) => readSource(id as UsageLimitSourceId, config),
118 { concurrency: 4 },
119 );
120 yield* publish(snapshots);
121 }).pipe(refreshLock.withPermits(1), Effect.ignoreCause({ log: true }));
122​
123 // Shares the refresh lock so a stale in-flight read cannot overwrite a redemption.
124 const consumeResetCredit = Effect.fn("UsageLimitSources.consumeResetCredit")(function* (
125 input: UsageLimitSourceConsumeResetCreditInput,
126 ) {
127 const settings = yield* settingsService.getSettings.pipe(
128 Effect.mapError(() => new UsageLimitSourceError({ detail: "Could not read hub settings." })),
129 );
130 const config = settings.usageLimitSources[input.sourceId];
131 if (!config?.enabled) {
132 return yield* new UsageLimitSourceError({
133 detail: "The usage limit source is missing or disabled.",
134 });
135 }
136 const managementKey = yield* readManagementKey(config);
137 if (managementKey.length === 0) {
138 return yield* new UsageLimitSourceError({ detail: missingManagementKey(config) });
139 }
140 const result = yield* api.consume(
141 { url: config.url, managementKey },
142 input.accountId,
143 input.creditId,
144 );
145 const snapshot = yield* readSource(input.sourceId, config);
146 const previous = yield* Ref.get(stateRef);
147 yield* publish(previous.map((source) => (source.id === input.sourceId ? snapshot : source)));
148 return result;
149 }, refreshLock.withPermits(1));
150​
151 yield* settingsService.streamChanges.pipe(
152 Stream.map((settings) => settings.usageLimitSources),
153 Stream.changes,
154 Stream.runForEach(() => refresh),
155 Effect.forkScoped,
156 );
157​
158 const interval = settingsService.getSettings.pipe(
159 Effect.map(
160 (settings) => resolveServerBackgroundActivitySettings(settings).providerHealthRefreshInterval,
161 ),
162 Effect.orElseSucceed(() => DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL),
163 );
164 yield* Effect.forever(
165 interval.pipe(
166 Effect.flatMap((wait) => Effect.sleep(Duration.isPositive(wait) ? wait : "60 seconds")),
167 Effect.andThen(interval),
168 Effect.flatMap((current) =>
169 Duration.isPositive(current)
170 ? backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })
171 : Effect.succeed(false),
172 ),
173 Effect.flatMap((shouldRun) => (shouldRun ? refresh : Effect.void)),
174 Effect.ignoreCause({ log: true }),
175 ),
176 ).pipe(Effect.forkScoped);
177​
178 yield* refresh.pipe(Effect.forkScoped);
179​
180 return {
181 current: Ref.get(stateRef),
182 consumeResetCredit,
183 refresh,
184 get streamChanges() {
185 return Stream.unwrap(
186 Effect.gen(function* () {
187 const subscription = yield* PubSub.subscribe(changes);
188 const snapshot = yield* Ref.get(stateRef);
189 return Stream.concat(Stream.make(snapshot), Stream.fromSubscription(subscription)).pipe(
190 Stream.changes,
191 );
192 }),
193 );
194 },
195 } satisfies UsageLimitSources["Service"];
196});
197​
198export const layer = Layer.effect(UsageLimitSources, make).pipe(
199 Layer.provide(SecretResolverLayer.layer),
200);