@@ -2,7 +2,6 @@ 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" ;
65import * as Duration from "effect/Duration" ;
76import * as Effect from "effect/Effect" ;
87import * as Exit from "effect/Exit" ;
@@ -466,17 +465,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
466465 generation : number ,
467466 lastFailure : ConnectionAttemptError | null ,
468467 synchronizedRoutes : Ref . Ref < number > ,
469- transportOpened : Deferred . Deferred < void > ,
470- routeOpen : Ref . Ref < boolean > ,
468+ routeOpen : SubscriptionRef . SubscriptionRef < boolean > ,
471469 ) {
472470 return yield * driver . connect ( yield * attemptEntry , ( progress ) => {
473471 if ( "transportOpened" in progress ) {
474- return Ref . set ( routeOpen , true ) . pipe (
475- Effect . andThen ( Deferred . succeed ( transportOpened , undefined ) ) ,
476- Effect . asVoid ,
477- ) ;
472+ return SubscriptionRef . set ( routeOpen , true ) ;
478473 }
479- return Ref . set ( routeOpen , false ) . pipe (
474+ return SubscriptionRef . set ( routeOpen , false ) . pipe (
480475 Effect . andThen (
481476 progress . stage === "synchronizing"
482477 ? Ref . update ( synchronizedRoutes , ( count ) => count + 1 )
@@ -565,19 +560,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
565560 lastFailure : ConnectionAttemptError | null ,
566561 pendingRetry : Option . Option < PendingRetryTrace > ,
567562 synchronizedRoutes : Ref . Ref < number > ,
568- transportOpened : Deferred . Deferred < void > ,
569- routeOpen : Ref . Ref < boolean > ,
563+ routeOpen : SubscriptionRef . SubscriptionRef < boolean > ,
570564 ) {
571565 if ( usesRelay ) {
572566 return yield * traceRelayEstablishment (
573- establishConnection (
574- attempt ,
575- generation ,
576- lastFailure ,
577- synchronizedRoutes ,
578- transportOpened ,
579- routeOpen ,
580- ) ,
567+ establishConnection ( attempt , generation , lastFailure , synchronizedRoutes , routeOpen ) ,
581568 attempt ,
582569 generation ,
583570 pendingRetry ,
@@ -588,7 +575,6 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
588575 generation ,
589576 lastFailure ,
590577 synchronizedRoutes ,
591- transportOpened ,
592578 routeOpen ,
593579 ) . pipe (
594580 Effect . map ( ( lease ) => ( {
@@ -602,7 +588,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
602588 ) ;
603589 } ) ;
604590
605- const waitForEstablishmentInterrupt = Effect . fnUntraced ( function * ( routeOpen : Ref . Ref < boolean > ) {
591+ const waitForEstablishmentInterrupt = Effect . fnUntraced ( function * (
592+ routeOpen : SubscriptionRef . SubscriptionRef < boolean > ,
593+ ) {
606594 for ( ; ; ) {
607595 const next = yield * Queue . take ( signals ) ;
608596 switch ( next . _tag ) {
@@ -615,7 +603,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
615603 }
616604 // An attempt started before the network came back can hang until
617605 // its setup timeout, so a recovery restarts it unless its socket is open.
618- if ( next . network === "online" && ! ( yield * Ref . get ( routeOpen ) ) ) {
606+ if ( next . network === "online" && ! ( yield * SubscriptionRef . get ( routeOpen ) ) ) {
619607 return true ;
620608 }
621609 break ;
@@ -885,14 +873,22 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
885873 }
886874 } ) ;
887875
876+ // An open socket pauses the clock; a route that fails after opening gives
877+ // the next route its own time to open.
888878 const awaitWakeRecoveryStall = Effect . fnUntraced ( function * (
889- transportOpened : Deferred . Deferred < void > ,
879+ routeOpen : SubscriptionRef . SubscriptionRef < boolean > ,
890880 ) {
891- const opened = yield * Deferred . await ( transportOpened ) . pipe (
892- Effect . timeoutOption ( WAKE_RECOVERY_OPEN_TIMEOUT ) ,
893- ) ;
894- if ( Option . isSome ( opened ) ) {
895- return yield * Effect . never ;
881+ const awaitRouteOpen = ( open : boolean ) =>
882+ SubscriptionRef . changes ( routeOpen ) . pipe (
883+ Stream . filter ( ( value ) => value === open ) ,
884+ Stream . runHead ,
885+ ) ;
886+ while (
887+ Option . isSome (
888+ yield * awaitRouteOpen ( true ) . pipe ( Effect . timeoutOption ( WAKE_RECOVERY_OPEN_TIMEOUT ) ) ,
889+ )
890+ ) {
891+ yield * awaitRouteOpen ( false ) ;
896892 }
897893 yield * Effect . logInfo ( "Connection after a wake did not open; retrying it once." ) . pipe (
898894 Effect . annotateLogs ( {
@@ -913,8 +909,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
913909 const switchingTo = yield * Ref . get ( preferredRouteId ) ;
914910 yield * SubscriptionRef . set ( prepared , Option . none ( ) ) ;
915911 const synchronizedRoutes = yield * Ref . make ( 0 ) ;
916- const transportOpened = yield * Deferred . make < void > ( ) ;
917- const routeOpen = yield * Ref . make ( false ) ;
912+ const routeOpen = yield * SubscriptionRef . make ( false ) ;
918913 const wakeRecovery = yield * Ref . getAndSet ( wakeRecoveryPending , false ) ;
919914 const establishment = yield * Effect . raceAllFirst ( [
920915 exitUnlessInterrupted (
@@ -924,7 +919,6 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
924919 lastFailure ,
925920 pendingRetry ,
926921 synchronizedRoutes ,
927- transportOpened ,
928922 routeOpen ,
929923 ) ,
930924 ) . pipe (
@@ -957,7 +951,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
957951 yield * Effect . sleep ( Duration . nanos ( remaining ) ) ;
958952 }
959953 } ) ,
960- ...( wakeRecovery ? [ awaitWakeRecoveryStall ( transportOpened ) ] : [ ] ) ,
954+ ...( wakeRecovery ? [ awaitWakeRecoveryStall ( routeOpen ) ] : [ ] ) ,
961955 ] ) ;
962956
963957 if ( establishment . _tag === "Interrupted" ) {
0 commit comments