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

serverActivation.ts

79 lines2.8 KB

1import * as Context from "effect/Context";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import type * as Scope from "effect/Scope";
6​
7export class ServerActivation extends Context.Reference<Effect.Effect<void> | undefined>(
8 "t3/serverActivation",
9 { defaultValue: () => undefined },
10) {}
11​
12export const forkParkedFiber = <A, E, R>(
13 effect: Effect.Effect<A, E, R>,
14): Effect.Effect<Fiber.Fiber<A, E>, never, Scope.Scope | R> =>
15 Effect.gen(function* () {
16 const activation = yield* ServerActivation;
17 if (activation === undefined) {
18 return yield* Effect.forkScoped(effect);
19 }
20 const parked = yield* Deferred.make<void>();
21 const fiber = yield* Effect.forkScoped(
22 Deferred.succeed(parked, undefined).pipe(Effect.andThen(activation), Effect.andThen(effect)),
23 );
24 yield* Deferred.await(parked);
25 return fiber;
26 });
27​
28export const forkParked = <A, E, R>(
29 effect: Effect.Effect<A, E, R>,
30): Effect.Effect<void, never, Scope.Scope | R> => forkParkedFiber(effect).pipe(Effect.asVoid);
31​
32export interface BackgroundStart {
33 readonly awaitFirstClient: Effect.Effect<void>;
34 readonly awaitTurn: Effect.Effect<void>;
35 readonly markClientServed: Effect.Effect<void>;
37​
38export class ServerBackgroundStart extends Context.Reference<BackgroundStart | undefined>(
39 "t3/serverBackgroundStart",
40 { defaultValue: () => undefined },
41) {}
42​
43// Probes wait for config delivery (at most 10s). Maintenance also waits for a
44// quiet startup (10s, at most 30s).
45export const makeBackgroundStart = (activation: Effect.Effect<void>) =>
46 Effect.gen(function* () {
47 const clientServed = yield* Deferred.make<void>();
48 const served = Deferred.await(clientServed);
49 const firstClient = yield* activation.pipe(
50 Effect.andThen(served.pipe(Effect.timeoutOption("10 seconds"))),
51 Effect.forkScoped,
52 );
53 const quiet = yield* activation.pipe(
54 Effect.andThen(
55 Effect.all([Effect.sleep("10 seconds"), served], { concurrency: "unbounded" }).pipe(
56 Effect.timeoutOption("30 seconds"),
57 ),
58 ),
59 Effect.forkScoped,
60 );
61 return {
62 awaitFirstClient: Fiber.join(firstClient).pipe(Effect.asVoid),
63 awaitTurn: Fiber.join(quiet).pipe(Effect.asVoid),
64 markClientServed: Deferred.succeed(clientServed, undefined).pipe(Effect.asVoid),
65 } satisfies BackgroundStart;
66 });
67​
68export const awaitBackgroundTurn = Effect.gen(function* () {
69 const gate = yield* ServerBackgroundStart;
70 if (gate !== undefined) yield* gate.awaitTurn;
71});
72​
73export const markClientServed = Effect.gen(function* () {
74 const gate = yield* ServerBackgroundStart;
75 if (gate !== undefined) yield* gate.markClientServed;
76});
77​
78export const forkBackground = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
79 forkParked(Effect.andThen(awaitBackgroundTurn, effect));