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

ProviderInstanceRegistry.ts

87 lines4.0 KB

1/**
2 * ProviderInstanceRegistry — the single Effect service in the new model.
3 *
4 * Owns a `Map<ProviderInstanceId, ProviderInstance>` produced by running
5 * registered driver factories against `ServerSettings.providerInstances`.
6 * The registry watches settings; when an instance's config changes (or
7 * the entry disappears), the registry tears down the affected instance's
8 * scope and rebuilds — that's the entire hot-reload story.
9 *
10 * What rest-of-server reads from here:
11 * - `getInstance(instanceId)` — for routing turn/session calls.
12 * - `listInstances` — for snapshot aggregation in `ProviderRegistry`.
13 * - `listUnavailable` — `ServerProvider` shadows for instances whose
14 * driver is not registered in this build (rollback / fork tolerance).
15 * - `streamChanges` — coalesced "registry mutated" pings so consumers
16 * can re-pull lists or re-broadcast.
17 *
18 * @module provider/Services/ProviderInstanceRegistry
19 */
20import type { ProviderInstanceId, ServerProvider } from "@t3tools/contracts";
21import * as Context from "effect/Context";
22import type * as Effect from "effect/Effect";
23import type * as PubSub from "effect/PubSub";
24import type * as Scope from "effect/Scope";
25import type * as Stream from "effect/Stream";
26​
27import type { ProviderInstance } from "../ProviderDriver.ts";
28​
29export interface ProviderInstanceRegistryShape {
30 /**
31 * Look up one instance by id. Returns `undefined` (not Option) when the
32 * id is unknown — callers branch on falsy and emit
33 * `ProviderInstanceNotFoundError`.
34 */
35 readonly getInstance: (
36 instanceId: ProviderInstanceId,
37 ) => Effect.Effect<ProviderInstance | undefined>;
38 /**
39 * Every available (driver-registered, successfully created) instance,
40 * in stable settings-author order.
41 */
42 readonly listInstances: Effect.Effect<ReadonlyArray<ProviderInstance>>;
43 /**
44 * Wire-shape shadow snapshots for instances whose driver is unknown to
45 * this build (or whose config failed to decode). Suitable for merging
46 * directly into `ProviderRegistry` output.
47 */
48 readonly listUnavailable: Effect.Effect<ReadonlyArray<ServerProvider>>;
49 /**
50 * Push notification stream emitted whenever the registry's contents
51 * change — instance added, removed, or rebuilt. The payload is `void`
52 * because consumers always want to re-pull `listInstances` /
53 * `listUnavailable` together.
54 *
55 * NOTE: because `Stream.fromPubSub` defers `PubSub.subscribe` until the
56 * stream starts running, forking a consumer via
57 * `Stream.runForEach(...).pipe(Effect.forkScoped)` races the next
58 * publish — the forked fiber may not have subscribed yet when the
59 * publish lands. Hot-reload consumers that must not miss a publish
60 * should use `subscribeChanges` below instead, which acquires the
61 * subscription synchronously in the caller's fiber before the consumer
62 * loop is forked.
63 */
64 readonly streamChanges: Stream.Stream<void>;
65 /**
66 * Acquire a subscription to the registry's change channel synchronously
67 * in the caller's fiber. Returns a `PubSub.Subscription<void>` whose
68 * lifetime is scoped to the provided `Scope` (the subscription is
69 * released when the scope closes). Consumers typically `yield*` this
70 * in the same fiber that forks their consumer loop, then drain with
71 * `PubSub.take(subscription)` inside `Effect.forever`. Because the
72 * subscription is registered with the PubSub before this `yield*`
73 * returns, no subsequent publish can land in a gap.
74 *
75 * This exists because the `ProviderInstanceRegistry` publishes on a
76 * PubSub and `Stream.fromPubSub` defers subscription until the stream
77 * starts executing — a consumer that `forkScoped`s the stream
78 * consumption can miss a publish that lands in the narrow window
79 * between "fiber scheduled" and "fiber starts running".
80 */
81 readonly subscribeChanges: Effect.Effect<PubSub.Subscription<void>, never, Scope.Scope>;
83​
84export class ProviderInstanceRegistry extends Context.Service<
85 ProviderInstanceRegistry,
86 ProviderInstanceRegistryShape
87>()("t3/provider/Services/ProviderInstanceRegistry") {}