Skip to content

Commit 8f22839

Browse files
committed
fix(server): bound slow-client event buffers
1 parent 7839140 commit 8f22839

6 files changed

Lines changed: 786 additions & 105 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { it } from "@effect/vitest";
2+
import * as Deferred from "effect/Deferred";
3+
import * as Effect from "effect/Effect";
4+
import * as Queue from "effect/Queue";
5+
import * as Stream from "effect/Stream";
6+
import { describe, expect } from "vite-plus/test";
7+
8+
import { makeLiveStreamBudget, type RetainedLiveItem } from "./LiveStreamBudget.ts";
9+
10+
describe("LiveStreamBudget", () => {
11+
it.effect("closes the source without releasing a batch still waiting for an ACK", () =>
12+
Effect.scoped(
13+
Effect.gen(function* () {
14+
const budget = yield* makeLiveStreamBudget({ maxItems: 3 });
15+
const queue = yield* Queue.unbounded<RetainedLiveItem<{ text: string }>>();
16+
const sourceClosed = yield* Deferred.make<void>();
17+
const first = yield* budget.retain({ text: "first" });
18+
const second = yield* budget.retain({ text: "second" });
19+
const third = yield* budget.retain({ text: "third" });
20+
yield* Queue.offerAll(queue, [first, second, third]);
21+
yield* Effect.scoped(
22+
Effect.gen(function* () {
23+
const pull = yield* Stream.toPull(
24+
budget.deliver(
25+
Stream.fromQueue(queue).pipe(
26+
Stream.rechunk(1),
27+
Stream.ensuring(Deferred.succeed(sourceClosed, undefined)),
28+
),
29+
),
30+
);
31+
expect(yield* pull).toEqual([{ text: "first" }]);
32+
// The other two items are in the source's pull state, not its queue.
33+
expect(yield* Queue.size(queue)).toBe(0);
34+
expect((yield* budget.usage).retainedItems).toBe(3);
35+
const overflow = yield* budget.retain({ text: "fourth" }).pipe(Effect.result);
36+
expect(overflow._tag).toBe("Failure");
37+
// Do not resume the consumer. Its source scope must close now.
38+
yield* Deferred.await(sourceClosed);
39+
yield* budget.closed;
40+
expect((yield* budget.usage).retainedItems).toBe(1);
41+
}),
42+
);
43+
expect(yield* budget.usage).toEqual({ retainedItems: 0, retainedSerializedBytes: 0 });
44+
}),
45+
),
46+
);
47+
});
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import { OrchestrationGetSnapshotError } from "@t3tools/contracts";
2+
import * as Arr from "effect/Array";
3+
import * as Deferred from "effect/Deferred";
4+
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
6+
import * as Scope from "effect/Scope";
7+
import * as Stream from "effect/Stream";
8+
9+
export const LIVE_STREAM_MAX_ITEMS = 1_000;
10+
export const LIVE_STREAM_MAX_SERIALIZED_BYTES = 8 * 1024 * 1024;
11+
12+
export interface RetainedLiveItem<A> {
13+
readonly value: A;
14+
readonly serializedBytes: number;
15+
}
16+
17+
// Published events are immutable and shared across subscriptions. Measure each
18+
// object once without keeping the event or its serialized copy alive.
19+
const serializedSizes = new WeakMap<object, number>();
20+
21+
function serializedSize(value: object): number {
22+
const cached = serializedSizes.get(value);
23+
if (cached !== undefined) {
24+
return cached;
25+
}
26+
const bytes = Buffer.byteLength(JSON.stringify(value));
27+
serializedSizes.set(value, bytes);
28+
return bytes;
29+
}
30+
31+
/** One budget covers one subscription and its delivery stream, including the batch waiting for an RPC ACK. */
32+
export const makeLiveStreamBudget = Effect.fn("makeLiveStreamBudget")(function* (limits?: {
33+
readonly maxItems?: number;
34+
readonly maxSerializedBytes?: number;
35+
}) {
36+
const maxItems = limits?.maxItems ?? LIVE_STREAM_MAX_ITEMS;
37+
const maxSerializedBytes = limits?.maxSerializedBytes ?? LIVE_STREAM_MAX_SERIALIZED_BYTES;
38+
const failed = yield* Deferred.make<never, OrchestrationGetSnapshotError>();
39+
const cleanupComplete = yield* Deferred.make<void>();
40+
let failure: OrchestrationGetSnapshotError | undefined;
41+
const retained = new Set<RetainedLiveItem<unknown>>();
42+
let retainedSerializedBytes = 0;
43+
44+
const release = (items: Iterable<RetainedLiveItem<unknown>>) => {
45+
for (const item of items) {
46+
if (!retained.delete(item)) {
47+
continue;
48+
}
49+
retainedSerializedBytes -= item.serializedBytes;
50+
}
51+
};
52+
yield* Effect.addFinalizer(() =>
53+
Effect.sync(() => release(retained)).pipe(
54+
Effect.andThen(Deferred.succeed(cleanupComplete, undefined)),
55+
),
56+
);
57+
58+
const check = Effect.suspend(() => (failure ? Effect.fail(failure) : Effect.void));
59+
60+
const overflow = Effect.fn("LiveStreamBudget.overflow")(function* (
61+
nextItems: number,
62+
nextSerializedBytes: number,
63+
) {
64+
failure ??= new OrchestrationGetSnapshotError({
65+
message: "The live event buffer is full. Resume from the last received sequence.",
66+
});
67+
yield* Deferred.fail(failed, failure);
68+
yield* Effect.logWarning("orchestration live event buffer is full", {
69+
retainedItems: retained.size,
70+
retainedSerializedBytes,
71+
nextItems,
72+
nextSerializedBytes,
73+
maxItems,
74+
maxSerializedBytes,
75+
});
76+
return yield* failure;
77+
});
78+
79+
const retain = <A extends object>(value: A, payload: object = value) =>
80+
Effect.suspend(() => {
81+
if (failure) {
82+
return Effect.fail(failure);
83+
}
84+
const serializedBytes = serializedSize(payload);
85+
const nextItems = retained.size + 1;
86+
const nextSerializedBytes = retainedSerializedBytes + serializedBytes;
87+
if (nextItems > maxItems || nextSerializedBytes > maxSerializedBytes) {
88+
return overflow(nextItems, nextSerializedBytes);
89+
}
90+
const item = { value, serializedBytes };
91+
retained.add(item);
92+
retainedSerializedBytes = nextSerializedBytes;
93+
return Effect.succeed(item);
94+
});
95+
96+
// Replace one coalescing batch atomically. Both raw and projected payloads
97+
// count against the same budget, and discarded updates release their charge.
98+
const replace = <A extends object>(
99+
previous: ReadonlyArray<RetainedLiveItem<unknown>>,
100+
values: ReadonlyArray<A>,
101+
payload: (value: A) => object = (value) => value,
102+
) =>
103+
Effect.suspend(() => {
104+
if (failure) {
105+
return Effect.fail(failure);
106+
}
107+
const next = values.map((value) => ({
108+
value,
109+
serializedBytes: serializedSize(payload(value)),
110+
}));
111+
let nextItems = retained.size + next.length;
112+
let nextSerializedBytes =
113+
retainedSerializedBytes + next.reduce((sum, item) => sum + item.serializedBytes, 0);
114+
for (const item of previous) {
115+
if (retained.has(item)) {
116+
nextItems -= 1;
117+
nextSerializedBytes -= item.serializedBytes;
118+
}
119+
}
120+
if (nextItems > maxItems || nextSerializedBytes > maxSerializedBytes) {
121+
return overflow(nextItems, nextSerializedBytes);
122+
}
123+
release(previous);
124+
for (const item of next) {
125+
retained.add(item);
126+
}
127+
retainedSerializedBytes = nextSerializedBytes;
128+
return Effect.succeed(next);
129+
});
130+
131+
const deliver = <A, E, R>(stream: Stream.Stream<RetainedLiveItem<A>, E, R>) =>
132+
Stream.fromPull(
133+
Effect.gen(function* () {
134+
yield* check;
135+
const sourceScope = yield* Scope.fork(yield* Effect.scope);
136+
const source = {
137+
pull: yield* Stream.toPull(stream).pipe(Scope.provide(sourceScope)),
138+
};
139+
let inFlight: ReadonlyArray<RetainedLiveItem<A>> = [];
140+
yield* Effect.addFinalizer(() =>
141+
Effect.sync(() => {
142+
release(inFlight);
143+
inFlight = [];
144+
source.pull = Effect.interrupt;
145+
}),
146+
);
147+
yield* Deferred.await(failed).pipe(
148+
Effect.catchTags({
149+
OrchestrationGetSnapshotError: (error) =>
150+
Effect.sync(() => {
151+
// A grouped source can retain its own pending chunk. Close it
152+
// and release the pull closure without waiting for an RPC ACK.
153+
source.pull = Effect.interrupt;
154+
}).pipe(
155+
Effect.andThen(Scope.close(sourceScope, Exit.fail(error))),
156+
Effect.andThen(
157+
Effect.sync(() => {
158+
const delivered = new Set<RetainedLiveItem<unknown>>(inFlight);
159+
for (const item of retained) {
160+
if (!delivered.has(item)) {
161+
release([item]);
162+
}
163+
}
164+
}),
165+
),
166+
Effect.andThen(Deferred.succeed(cleanupComplete, undefined)),
167+
),
168+
}),
169+
Effect.forkScoped,
170+
);
171+
// @effect-diagnostics-next-line returnEffectInGen:off - Stream.fromPull needs the pull effect as its result.
172+
return Effect.gen(function* () {
173+
// RpcServer requests the next batch only after the client ACKs this
174+
// one. Removing items from a queue alone does not mean delivery ended.
175+
release(inFlight);
176+
inFlight = [];
177+
yield* check;
178+
const items = yield* Effect.raceFirst(source.pull, Deferred.await(failed));
179+
inFlight = items;
180+
yield* check;
181+
return Arr.map(items, (item) => item.value);
182+
});
183+
}),
184+
);
185+
186+
return {
187+
retain,
188+
replace,
189+
release,
190+
deliver,
191+
check,
192+
failed: Deferred.await(failed),
193+
closed: Deferred.await(cleanupComplete),
194+
usage: Effect.sync(() => ({ retainedItems: retained.size, retainedSerializedBytes })),
195+
};
196+
});

0 commit comments

Comments
 (0)