|
| 1 | +/** |
| 2 | + * ProviderRateLimitsReactorLive - runtime stream -> provider snapshot. |
| 3 | + * |
| 4 | + * Adapters normalize their native plan-limit payload and attach it to |
| 5 | + * `account.rate-limits.updated`; this reactor is the only thing that knows |
| 6 | + * where that observation ends up. Events without a normalized snapshot (a |
| 7 | + * provider shape we cannot map yet) are ignored — they are still logged as raw |
| 8 | + * runtime events for diagnostics. |
| 9 | + * |
| 10 | + * @module ProviderRateLimitsReactorLive |
| 11 | + */ |
| 12 | +import type { ProviderRuntimeEvent } from "@t3tools/contracts"; |
| 13 | +import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker"; |
| 14 | +import * as Cause from "effect/Cause"; |
| 15 | +import * as Effect from "effect/Effect"; |
| 16 | +import * as Layer from "effect/Layer"; |
| 17 | +import * as Stream from "effect/Stream"; |
| 18 | + |
| 19 | +import { ProviderRegistry } from "../../provider/Services/ProviderRegistry.ts"; |
| 20 | +import { ProviderService } from "../../provider/Services/ProviderService.ts"; |
| 21 | +import { forkParked } from "../../serverActivation.ts"; |
| 22 | +import { |
| 23 | + ProviderRateLimitsReactor, |
| 24 | + type ProviderRateLimitsReactorShape, |
| 25 | +} from "../Services/ProviderRateLimitsReactor.ts"; |
| 26 | + |
| 27 | +type AccountRateLimitsUpdatedEvent = Extract< |
| 28 | + ProviderRuntimeEvent, |
| 29 | + { type: "account.rate-limits.updated" } |
| 30 | +>; |
| 31 | + |
| 32 | +export const make = Effect.gen(function* () { |
| 33 | + const providerService = yield* ProviderService; |
| 34 | + const providerRegistry = yield* ProviderRegistry; |
| 35 | + |
| 36 | + const processEvent = (event: AccountRateLimitsUpdatedEvent) => { |
| 37 | + const snapshot = event.payload.snapshot; |
| 38 | + const instanceId = event.providerInstanceId; |
| 39 | + // `ProviderService` stamps the instance id on every event it fans out; an |
| 40 | + // event without one predates that and cannot be attributed to a snapshot. |
| 41 | + if (!snapshot || instanceId === undefined) { |
| 42 | + return Effect.void; |
| 43 | + } |
| 44 | + return providerRegistry.setProviderRateLimits({ instanceId, rateLimits: snapshot }).pipe( |
| 45 | + Effect.asVoid, |
| 46 | + Effect.catchCause((cause) => |
| 47 | + Cause.hasInterruptsOnly(cause) |
| 48 | + ? Effect.failCause(cause) |
| 49 | + : Effect.logWarning("failed to record provider rate limits", { |
| 50 | + instanceId, |
| 51 | + provider: event.provider, |
| 52 | + cause: Cause.pretty(cause), |
| 53 | + }), |
| 54 | + ), |
| 55 | + ); |
| 56 | + }; |
| 57 | + |
| 58 | + const worker = yield* makeDrainableWorker(processEvent); |
| 59 | + |
| 60 | + const start: ProviderRateLimitsReactorShape["start"] = () => |
| 61 | + forkParked( |
| 62 | + Stream.runForEach(providerService.streamEvents, (event) => |
| 63 | + event.type === "account.rate-limits.updated" ? worker.enqueue(event) : Effect.void, |
| 64 | + ), |
| 65 | + ).pipe(Effect.asVoid); |
| 66 | + |
| 67 | + return { |
| 68 | + start, |
| 69 | + drain: worker.drain, |
| 70 | + } satisfies ProviderRateLimitsReactorShape; |
| 71 | +}); |
| 72 | + |
| 73 | +export const ProviderRateLimitsReactorLive = Layer.effect(ProviderRateLimitsReactor, make); |
0 commit comments