@@ -2,6 +2,7 @@ import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
22import * as Cause from "effect/Cause" ;
33import * as Clock from "effect/Clock" ;
44import * as Context from "effect/Context" ;
5+ import * as Deferred from "effect/Deferred" ;
56import * as Duration from "effect/Duration" ;
67import * as Effect from "effect/Effect" ;
78import * as Exit from "effect/Exit" ;
@@ -33,7 +34,14 @@ import * as ConnectionWakeups from "./wakeups.ts";
3334
3435const RETRY_BASE_DELAY_MS = 1_000 ;
3536const 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.
3643const CONNECTION_ESTABLISHMENT_TIMEOUT = "15 seconds" ;
44+ const CONNECTION_SYNCHRONIZATION_TIMEOUT = "75 seconds" ;
3745const 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