Skip to content

Commit 592d051

Browse files
fix(client-runtime): give the server config snapshot its own connect budget
The 15 second establishment timeout covered the whole connect attempt, including the wait for the first server-config snapshot. A cold server with a large database can take longer than that to answer, so the client tore down a healthy websocket and retried in a loop. Keep 15 seconds for resolving credentials and creating the session, then allow 75 more seconds for the socket to open (already capped at 15) and the snapshot to arrive. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent fed41fa commit 592d051

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎packages/client-runtime/src/connection/supervisor.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,29 @@ describe("EnvironmentSupervisor", () => {
436436
}).pipe(Effect.provide(TestClock.layer())),
437437
);
438438

439+
it.effect("keeps a working socket open while a slow server config snapshot arrives", () =>
440+
Effect.gen(function* () {
441+
const harness = yield* makeHarness({
442+
ready: () => Effect.sleep("40 seconds"),
443+
});
444+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
445+
initiallyDesired: true,
446+
}).pipe(Effect.provide(harness.dependencies));
447+
448+
yield* awaitState(
449+
supervisor.state,
450+
(state) => state.phase === "connecting" && state.stage === "synchronizing",
451+
);
452+
yield* TestClock.adjust("39 seconds");
453+
expect((yield* SubscriptionRef.get(supervisor.state)).phase).toBe("connecting");
454+
455+
yield* TestClock.adjust("1 second");
456+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
457+
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
458+
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
459+
}).pipe(Effect.provide(TestClock.layer())),
460+
);
461+
439462
it.effect("retries when a session never becomes ready", () =>
440463
Effect.gen(function* () {
441464
const harness = yield* makeHarness({
@@ -449,7 +472,7 @@ describe("EnvironmentSupervisor", () => {
449472
supervisor.state,
450473
(state) => state.phase === "connecting" && state.stage === "synchronizing",
451474
);
452-
yield* TestClock.adjust("14 seconds");
475+
yield* TestClock.adjust("74 seconds");
453476
expect((yield* SubscriptionRef.get(supervisor.state)).stage).toBe("synchronizing");
454477

455478
yield* TestClock.adjust("1 second");

‎packages/client-runtime/src/connection/supervisor.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
22
import * as Cause from "effect/Cause";
33
import * as Clock from "effect/Clock";
44
import * as Context from "effect/Context";
5+
import * as Deferred from "effect/Deferred";
56
import * as Duration from "effect/Duration";
67
import * as Effect from "effect/Effect";
78
import * as Exit from "effect/Exit";
@@ -33,7 +34,14 @@ import * as ConnectionWakeups from "./wakeups.ts";
3334

3435
const RETRY_BASE_DELAY_MS = 1_000;
3536
const RETRY_MAX_DELAY_MS = 300_000;
37+
// Budget to resolve credentials and create the session, which ends when the
38+
// driver reports the synchronizing stage. The socket then opens (bounded at 15
39+
// seconds by the RPC session) and the server gets the rest of its own budget
40+
// to deliver the first config snapshot. A cold server with a large database
41+
// can take longer than 15 seconds to answer, and that is not a reason to drop
42+
// a working connection.
3643
const CONNECTION_ESTABLISHMENT_TIMEOUT = "15 seconds";
44+
const CONNECTION_SYNCHRONIZATION_TIMEOUT = "75 seconds";
3745
const CONNECTION_PROBE_TIMEOUT = "15 seconds";
3846
// Mobile resumes, explicit retries, and offline events want a fast answer:
3947
// the user is waiting, or the network may be gone.
@@ -308,9 +316,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
308316
attempt: number,
309317
generation: number,
310318
lastFailure: ConnectionAttemptError | null,
319+
synchronizing: Deferred.Deferred<void>,
311320
) {
312321
return yield* driver.connect(entry, (progress) =>
313-
reportProgress(attempt, generation, lastFailure, progress),
322+
(progress.stage === "synchronizing"
323+
? Deferred.succeed(synchronizing, undefined)
324+
: Effect.void
325+
).pipe(Effect.andThen(reportProgress(attempt, generation, lastFailure, progress))),
314326
);
315327
});
316328

@@ -361,16 +373,17 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
361373
generation: number,
362374
lastFailure: ConnectionAttemptError | null,
363375
pendingRetry: Option.Option<PendingRetryTrace>,
376+
synchronizing: Deferred.Deferred<void>,
364377
) {
365378
if (target._tag === "RelayConnectionTarget") {
366379
return yield* traceRelayEstablishment(
367-
establishConnection(attempt, generation, lastFailure),
380+
establishConnection(attempt, generation, lastFailure, synchronizing),
368381
attempt,
369382
generation,
370383
pendingRetry,
371384
);
372385
}
373-
return yield* establishConnection(attempt, generation, lastFailure).pipe(
386+
return yield* establishConnection(attempt, generation, lastFailure, synchronizing).pipe(
374387
Effect.map((lease) => ({
375388
attemptSpan: Option.none<Tracer.Span>(),
376389
lease,
@@ -533,9 +546,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
533546
ignoreOffline: boolean,
534547
) {
535548
yield* SubscriptionRef.set(prepared, Option.none());
549+
const synchronizing = yield* Deferred.make<void>();
536550
const establishment = yield* Effect.raceAllFirst([
537551
exitUnlessInterrupted(
538-
establishTracedConnection(attempt, generation, lastFailure, pendingRetry),
552+
establishTracedConnection(attempt, generation, lastFailure, pendingRetry, synchronizing),
539553
).pipe(
540554
Effect.map((exit): EstablishmentEvent => ({
541555
_tag: "Completed",
@@ -548,9 +562,15 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
548562
resetRetry,
549563
})),
550564
),
551-
Effect.sleep(CONNECTION_ESTABLISHMENT_TIMEOUT).pipe(
552-
Effect.as<EstablishmentEvent>({ _tag: "TimedOut" }),
553-
),
565+
Effect.gen(function* () {
566+
const reachedSynchronizing = yield* Deferred.await(synchronizing).pipe(
567+
Effect.timeoutOption(CONNECTION_ESTABLISHMENT_TIMEOUT),
568+
);
569+
if (Option.isSome(reachedSynchronizing)) {
570+
yield* Effect.sleep(CONNECTION_SYNCHRONIZATION_TIMEOUT);
571+
}
572+
return { _tag: "TimedOut" } as const satisfies EstablishmentEvent;
573+
}),
554574
]);
555575

556576
if (establishment._tag === "Interrupted") {

0 commit comments

Comments
 (0)