fix(server): bound subscription event catch-up
This commit is contained in:
1 parent
a279bca0f8
commit
05e6ead25a
3 files changed
+110
-8
No files matched your search
@@ -27,9 +27,8 @@ export interface OrchestrationEngineShape {
|
||||
*
|
||||
* @param fromSequenceExclusive - Sequence cursor (exclusive).
|
||||
* @param limit - Maximum number of events to read. Defaults to the event
|
||||
* store's page-bounded default; pass a higher value when the caller must
|
||||
* read every event after the cursor (e.g. per-thread catch-up that filters
|
||||
* a small subset out of a potentially larger global range).
|
||||
* store's page-bounded default. Callers must keep this bounded; use a
|
||||
* projection snapshot instead of replaying an arbitrarily stale cursor.
|
||||
* @returns Stream containing ordered events.
|
||||
*/
|
||||
readonly readEvents: (
|
||||
|
||||
@@ -5877,6 +5877,52 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("subscribeThread replaces a stale cursor with a fresh snapshot", () =>
|
||||
Effect.gen(function* () {
|
||||
const snapshotSequence = 5_000;
|
||||
const thread = makeDefaultOrchestrationReadModel().threads[0]!;
|
||||
let replayCalls = 0;
|
||||
|
||||
yield* buildAppUnderTest({
|
||||
layers: {
|
||||
projectionSnapshotQuery: {
|
||||
getSnapshotSequence: () => Effect.succeed({ snapshotSequence }),
|
||||
getThreadDetailSnapshot: () =>
|
||||
Effect.succeed(
|
||||
Option.some({
|
||||
snapshotSequence,
|
||||
thread,
|
||||
}),
|
||||
),
|
||||
},
|
||||
orchestrationEngine: {
|
||||
readEvents: () => {
|
||||
replayCalls += 1;
|
||||
return Stream.empty;
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const wsUrl = yield* getWsServerUrl("/ws");
|
||||
const result = yield* Effect.scoped(
|
||||
withWsRpcClient(wsUrl, (client) =>
|
||||
client[ORCHESTRATION_WS_METHODS.subscribeThread]({
|
||||
threadId: defaultThreadId,
|
||||
afterSequence: 1,
|
||||
}).pipe(Stream.take(1), Stream.runCollect),
|
||||
),
|
||||
);
|
||||
|
||||
assert.equal(replayCalls, 0);
|
||||
assert.equal(result[0]?.kind, "snapshot");
|
||||
if (result[0]?.kind === "snapshot") {
|
||||
assert.equal(result[0].snapshot.snapshotSequence, snapshotSequence);
|
||||
assert.equal(result[0].snapshot.thread.id, defaultThreadId);
|
||||
}
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("subscribeShell coalesces a per-thread burst without stalling other threads", () =>
|
||||
Effect.gen(function* () {
|
||||
const busyThreadId = ThreadId.make("thread-busy");
|
||||
|
||||
+62
-5
@@ -116,6 +116,24 @@ import * as PairingGrantStore from "./auth/PairingGrantStore.ts";
|
||||
import * as SessionStore from "./auth/SessionStore.ts";
|
||||
import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts";
|
||||
import * as RelayClient from "@t3tools/shared/relayClient";
|
||||
|
||||
const ORCHESTRATION_SUBSCRIPTION_REPLAY_LIMIT = 1_000;
|
||||
|
||||
const subscriptionReplayLimit = (
|
||||
afterSequence: number,
|
||||
snapshotSequence: number,
|
||||
): number | null => {
|
||||
const eventCount = snapshotSequence - afterSequence;
|
||||
if (
|
||||
!Number.isSafeInteger(eventCount) ||
|
||||
eventCount < 0 ||
|
||||
eventCount > ORCHESTRATION_SUBSCRIPTION_REPLAY_LIMIT
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return eventCount;
|
||||
};
|
||||
|
||||
const isOrchestrationDispatchCommandError = Schema.is(OrchestrationDispatchCommandError);
|
||||
|
||||
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
|
||||
@@ -1398,14 +1416,53 @@ const makeWsRpcLayer = (
|
||||
// catch-up followed by the buffered/ongoing live events. Overlapping
|
||||
// events are deduped by sequence on the client.
|
||||
//
|
||||
// Read the full range after the cursor (not the store's default
|
||||
// page-bounded limit): the range is normally tiny (a fresh HTTP
|
||||
// snapshot sequence) and the per-thread filter runs after reading,
|
||||
// so a global cap could otherwise omit this thread's events.
|
||||
// A recent cursor replays the exact global range through the
|
||||
// per-thread filter. A stale cursor receives a current thread
|
||||
// snapshot instead, keeping catch-up bounded even when the global
|
||||
// event log is very large.
|
||||
if (input.afterSequence !== undefined) {
|
||||
const afterSequence = input.afterSequence;
|
||||
const { snapshotSequence } = yield* projectionSnapshotQuery
|
||||
.getSnapshotSequence()
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestrationGetSnapshotError({
|
||||
message: "Failed to load orchestration snapshot sequence",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const replayLimit = subscriptionReplayLimit(afterSequence, snapshotSequence);
|
||||
|
||||
if (replayLimit === null) {
|
||||
const snapshot = yield* projectionSnapshotQuery
|
||||
.getThreadDetailSnapshot(input.threadId)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestrationGetSnapshotError({
|
||||
message: `Failed to load thread ${input.threadId}`,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (Option.isNone(snapshot)) {
|
||||
return yield* new OrchestrationGetSnapshotError({
|
||||
message: `Thread ${input.threadId} was not found`,
|
||||
cause: input.threadId,
|
||||
});
|
||||
}
|
||||
|
||||
return Stream.concat(
|
||||
Stream.make({ kind: "snapshot" as const, snapshot: snapshot.value }),
|
||||
bufferedLiveStream,
|
||||
);
|
||||
}
|
||||
|
||||
const catchUpStream = orchestrationEngine
|
||||
.readEvents(afterSequence, Number.MAX_SAFE_INTEGER)
|
||||
.readEvents(afterSequence, replayLimit)
|
||||
.pipe(
|
||||
Stream.filter(isThisThreadDetailEvent),
|
||||
Stream.map((event) => ({ kind: "event" as const, event })),
|
||||
|
||||
Reference in new issue
Block a user