Skip to content

Commit 0427b5f

Browse files
committed
fix(server): stop a busy terminal from starving other terminals' persistence
1 parent 31c1c59 commit 0427b5f

2 files changed

Lines changed: 111 additions & 12 deletions

File tree

‎packages/shared/src/KeyedCoalescingWorker.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,97 @@ describe("makeKeyedCoalescingWorker", () => {
9494
}),
9595
),
9696
);
97+
98+
it.live("lets other keys progress while one key stays continuously dirty", () =>
99+
Effect.scoped(
100+
Effect.gen(function* () {
101+
const processed: string[] = [];
102+
const busyStarted = yield* Deferred.make<void>();
103+
const releaseBusy = yield* Deferred.make<void>();
104+
const quietProcessed = yield* Deferred.make<void>();
105+
106+
const worker = yield* makeKeyedCoalescingWorker<string, string, never, never>({
107+
merge: (_current, next) => next,
108+
process: (key, value) =>
109+
Effect.gen(function* () {
110+
processed.push(`${key}:${value}`);
111+
112+
if (value === "busy-1") {
113+
yield* Deferred.succeed(busyStarted, undefined).pipe(Effect.orDie);
114+
yield* Deferred.await(releaseBusy);
115+
}
116+
117+
if (key === "quiet") {
118+
yield* Deferred.succeed(quietProcessed, undefined).pipe(Effect.orDie);
119+
}
120+
}),
121+
});
122+
123+
yield* worker.enqueue("busy", "busy-1");
124+
yield* Deferred.await(busyStarted);
125+
126+
// While the busy key is mid-batch, it becomes dirty again and another
127+
// key arrives. The dirty key must go to the queue tail, not back into
128+
// the worker, or the quiet key's flush never runs.
129+
yield* worker.enqueue("busy", "busy-2");
130+
yield* worker.enqueue("quiet", "quiet-1");
131+
yield* Deferred.succeed(releaseBusy, undefined);
132+
133+
yield* Deferred.await(quietProcessed);
134+
yield* worker.drainKey("quiet");
135+
yield* worker.drainKey("busy");
136+
137+
expect(processed).toEqual(["busy:busy-1", "quiet:quiet-1", "busy:busy-2"]);
138+
}),
139+
),
140+
);
141+
142+
it.live("still coalesces updates that land while their key waits at the queue tail", () =>
143+
Effect.scoped(
144+
Effect.gen(function* () {
145+
const processed: string[] = [];
146+
const firstStarted = yield* Deferred.make<void>();
147+
const releaseFirst = yield* Deferred.make<void>();
148+
const otherStarted = yield* Deferred.make<void>();
149+
const releaseOther = yield* Deferred.make<void>();
150+
151+
const worker = yield* makeKeyedCoalescingWorker<string, string, never, never>({
152+
merge: (current, next) => `${current}+${next}`,
153+
process: (key, value) =>
154+
Effect.gen(function* () {
155+
processed.push(`${key}:${value}`);
156+
157+
if (value === "first") {
158+
yield* Deferred.succeed(firstStarted, undefined).pipe(Effect.orDie);
159+
yield* Deferred.await(releaseFirst);
160+
}
161+
162+
if (key === "other") {
163+
yield* Deferred.succeed(otherStarted, undefined).pipe(Effect.orDie);
164+
yield* Deferred.await(releaseOther);
165+
}
166+
}),
167+
});
168+
169+
yield* worker.enqueue("busy", "first");
170+
yield* Deferred.await(firstStarted);
171+
172+
// Dirty the busy key mid-batch so it gets requeued behind "other",
173+
// then keep writing to it while it waits at the tail. Every write must
174+
// fold into one merged batch, not extra queue entries.
175+
yield* worker.enqueue("busy", "a");
176+
yield* worker.enqueue("other", "other-1");
177+
yield* Deferred.succeed(releaseFirst, undefined);
178+
yield* Deferred.await(otherStarted);
179+
yield* worker.enqueue("busy", "b");
180+
yield* worker.enqueue("busy", "c");
181+
yield* Deferred.succeed(releaseOther, undefined);
182+
183+
yield* worker.drainKey("busy");
184+
yield* worker.drainKey("other");
185+
186+
expect(processed).toEqual(["busy:first", "other:other-1", "busy:a+b+c"]);
187+
}),
188+
),
189+
);
97190
});

‎packages/shared/src/KeyedCoalescingWorker.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,26 @@ export const makeKeyedCoalescingWorker = <K, V, E, R>(options: {
3939
options.process(key, value).pipe(
4040
Effect.flatMap(() =>
4141
TxRef.modify(stateRef, (state) => {
42-
const nextValue = state.latestByKey.get(key);
43-
if (nextValue === undefined) {
44-
const activeKeys = new Set(state.activeKeys);
45-
activeKeys.delete(key);
46-
return [null, { ...state, activeKeys }] as const;
42+
const activeKeys = new Set(state.activeKeys);
43+
activeKeys.delete(key);
44+
45+
if (!state.latestByKey.has(key)) {
46+
return [false, { ...state, activeKeys }] as const;
4747
}
4848

49-
const latestByKey = new Map(state.latestByKey);
50-
latestByKey.delete(key);
51-
return [nextValue, { ...state, latestByKey }] as const;
52-
}).pipe(Effect.tx),
53-
),
54-
Effect.flatMap((nextValue) =>
55-
nextValue === null ? Effect.void : processKey(key, nextValue),
49+
// Work arrived while this batch ran. Requeue the key at the tail
50+
// instead of processing it again here, so a continuously dirty
51+
// key (a terminal in a redraw loop) cannot monopolize the worker
52+
// and starve every other key's flush.
53+
const queuedKeys = new Set(state.queuedKeys);
54+
queuedKeys.add(key);
55+
return [true, { ...state, activeKeys, queuedKeys }] as const;
56+
}).pipe(
57+
Effect.flatMap((shouldRequeue) =>
58+
shouldRequeue ? TxQueue.offer(queue, key) : Effect.void,
59+
),
60+
Effect.tx,
61+
),
5662
),
5763
);
5864

0 commit comments

Comments
 (0)