Skip to content

Commit 4b9e7bb

Browse files
perf(client): skip redundant shell refresh on legacy resubscribe
A session that already received a snapshot no longer reloads the shell over HTTP before requesting the full socket snapshot legacy servers need, so foreground wakeups and retries pay one transfer instead of two. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent c0e437c commit 4b9e7bb

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎packages/client-runtime/src/state/shell-sync.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,12 @@ describe("environment shell synchronization", () => {
540540
{ id: "created-after-http-snapshot" },
541541
]);
542542
expect(yield* Ref.get(loaderCalls)).toBe(1);
543+
544+
// A same-session resubscribe still requests a full socket snapshot but
545+
// does not pay for another HTTP refresh first.
546+
yield* Queue.offer(wakeups, "application-active");
547+
expect(yield* Queue.take(subscribeInputs)).toEqual({});
548+
expect(yield* Ref.get(loaderCalls)).toBe(1);
543549
}),
544550
);
545551
});

‎packages/client-runtime/src/state/shell.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
7272
error: Option.none(),
7373
});
7474
const awaitingCompletion = yield* Ref.make(false);
75-
const lastAuthoritativeSession = yield* Ref.make<RpcSession | null>(null);
75+
const lastSnapshotSession = yield* Ref.make<RpcSession | null>(null);
7676
const activeSubscriptionSession = yield* Ref.make<RpcSession | null>(null);
7777
const persistence = yield* Queue.sliding<OrchestrationShellSnapshot>(1);
7878

@@ -177,7 +177,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
177177
if (receivedSnapshot) {
178178
const session = yield* Ref.get(activeSubscriptionSession);
179179
if (session !== null) {
180-
yield* Ref.set(lastAuthoritativeSession, session);
180+
yield* Ref.set(lastSnapshotSession, session);
181181
}
182182
}
183183
if (next.snapshot !== initial.snapshot && Option.isSome(next.snapshot)) {
@@ -207,12 +207,13 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
207207
// Foreground resubscriptions on the same live session can resume from
208208
// the in-memory cursor when the server marks replay completion. Older
209209
// servers need a full socket snapshot because an HTTP snapshot cannot
210-
// account for events committed before the socket subscription starts.
211-
const hasAuthoritativeSnapshot =
212-
supportsCompletionMarker && (yield* Ref.get(lastAuthoritativeSession)) === session;
210+
// account for events committed before the socket subscription starts,
211+
// but a session that already holds one skips the redundant HTTP reload.
212+
const sessionHasSnapshot = (yield* Ref.get(lastSnapshotSession)) === session;
213+
const hasAuthoritativeSnapshot = supportsCompletionMarker && sessionHasSnapshot;
213214
let canResume = hasAuthoritativeSnapshot;
214215
let current = yield* SubscriptionRef.get(state);
215-
if (!hasAuthoritativeSnapshot || Option.isNone(current.snapshot)) {
216+
if (!sessionHasSnapshot || Option.isNone(current.snapshot)) {
216217
const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe(
217218
Effect.flatMap(
218219
Option.match({

0 commit comments

Comments
 (0)