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

ProviderInstallationGate.ts

73 lines2.7 KB

1import type { ProviderInstanceId } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Semaphore from "effect/Semaphore";
6​
7// Executions share `room` and an update drains it. A Semaphore lets a smaller request overtake a
8// waiting larger one, so an update first holds `turnstile` and new executions queue behind it.
9interface InstanceGate {
10 readonly turnstile: Semaphore.Semaphore;
11 readonly room: Semaphore.Semaphore;
13​
14const ROOM_PERMITS = Number.MAX_SAFE_INTEGER;
15​
16export class ProviderInstallationGate extends Context.Service<
17 ProviderInstallationGate,
18 {
19 /** Runs provider work once no update of the instance's installation is running or waiting. */
20 readonly execute: <A, E, R>(
21 instanceId: ProviderInstanceId,
22 effect: Effect.Effect<A, E, R>,
23 ) => Effect.Effect<A, E, R>;
24 /** Runs an installation update after the instances' in-flight work, excluding new work. */
25 readonly update: <A, E, R>(
26 instanceIds: ReadonlyArray<ProviderInstanceId>,
27 effect: Effect.Effect<A, E, R>,
28 ) => Effect.Effect<A, E, R>;
29 }
30>()("t3/provider/ProviderInstallationGate") {}
31​
32export const layer = Layer.sync(ProviderInstallationGate, () => {
33 // Instances are few and configured, so their gates are kept for the server's lifetime.
34 const gates = new Map<ProviderInstanceId, InstanceGate>();
35 const gateFor = (instanceId: ProviderInstanceId) =>
36 Effect.sync(() => {
37 const existing = gates.get(instanceId);
38 if (existing) return existing;
39 const gate = {
40 turnstile: Semaphore.makeUnsafe(1),
41 room: Semaphore.makeUnsafe(ROOM_PERMITS),
42 };
43 gates.set(instanceId, gate);
44 return gate;
45 });
46​
47 const execute = Effect.fn("ProviderInstallationGate.execute")(function* <A, E, R>(
48 instanceId: ProviderInstanceId,
49 effect: Effect.Effect<A, E, R>,
50 ) {
51 const gate = yield* gateFor(instanceId);
52 yield* gate.turnstile.withPermits(1)(Effect.void);
53 return yield* gate.room.withPermits(1)(effect);
54 });
55​
56 const update = Effect.fn("ProviderInstallationGate.update")(function* <A, E, R>(
57 instanceIds: ReadonlyArray<ProviderInstanceId>,
58 effect: Effect.Effect<A, E, R>,
59 ) {
60 // A stable order keeps two updates that share instances from deadlocking.
61 const instanceGates = yield* Effect.forEach(Array.from(new Set(instanceIds)).sort(), gateFor);
62 const drained = instanceGates.reduceRight(
63 (inner, gate) => gate.room.withPermits(ROOM_PERMITS)(inner),
64 effect,
65 );
66 return yield* instanceGates.reduceRight(
67 (inner, gate) => gate.turnstile.withPermits(1)(inner),
68 drained,
69 );
70 });
71​
72 return ProviderInstallationGate.of({ execute, update });
73});