@@ -44,6 +44,9 @@ use openshell_core::transport_errors::is_expected_transport_close_status;
4444
4545const INITIAL_BACKOFF : Duration = Duration :: from_secs ( 1 ) ;
4646const MAX_BACKOFF : Duration = Duration :: from_secs ( 30 ) ;
47+ /// How long a shared host's session waits for the host to attach to a
48+ /// gateway replica before dialing another address.
49+ const GATEWAY_HOME_WAIT : Duration = Duration :: from_secs ( 5 ) ;
4750const CONFIG_APPLY_TIMEOUT : Duration = Duration :: from_mins ( 1 ) ;
4851
4952/// A stream-delivered configuration update awaiting application by the
@@ -129,6 +132,9 @@ pub struct SessionRuntimeContext {
129132 /// Publishes whether the current session pushes configuration, so the
130133 /// runtime stops polling while it does.
131134 pub config_push_updates : watch:: Sender < bool > ,
135+ /// Peer endpoint of the gateway replica serving this supervisor's shared
136+ /// host. When set, sessions dial it instead of the configured address.
137+ pub gateway_home : Option < watch:: Receiver < Option < String > > > ,
132138}
133139
134140/// Parse a gRPC endpoint URI into an OCSF `Endpoint` (host + port). Falls back
@@ -423,6 +429,7 @@ pub fn spawn_with_readiness(
423429 session_id_updates : runtime. session_id_updates ,
424430 config_apply_tx : runtime. config_apply_tx ,
425431 config_push_updates : runtime. config_push_updates ,
432+ gateway_home : runtime. gateway_home ,
426433 ready_tx,
427434 } ;
428435 ( tokio:: spawn ( run_session_loop ( config) ) , ready_rx)
@@ -442,6 +449,7 @@ struct SessionConfig {
442449 /// Publishes whether the current session pushes configuration. A
443450 /// reconnect to a gateway that does not push resumes polling.
444451 config_push_updates : watch:: Sender < bool > ,
452+ gateway_home : Option < watch:: Receiver < Option < String > > > ,
445453 ready_tx : watch:: Sender < bool > ,
446454}
447455
@@ -455,9 +463,22 @@ async fn run_session_loop(config: SessionConfig) {
455463 // back to the configured address, which load-balances across replicas.
456464 let mut target = config. gateway . clone ( ) ;
457465 let mut redirected = false ;
466+ // A shared host's sessions dial the replica serving the host, as
467+ // redirected so that replica serves them, unless they are following a
468+ // redirect away from it.
469+ let mut gateway_home = config. gateway_home . clone ( ) ;
470+ let mut follow_home = true ;
458471
459472 loop {
460473 attempt += 1 ;
474+ if follow_home
475+ && let Some ( home) = gateway_home. as_mut ( )
476+ && let Some ( endpoint) = wait_for_home ( home, None ) . await
477+ {
478+ target = config. gateway . with_url ( endpoint) ;
479+ redirected = true ;
480+ }
481+ follow_home = true ;
461482
462483 let result = run_single_session ( & config, & target, redirected, attempt) . await ;
463484 if let Some ( updates) = & config. session_id_updates {
@@ -485,8 +506,14 @@ async fn run_session_loop(config: SessionConfig) {
485506 peer_endpoint = %peer_endpoint,
486507 "supervisor session: following gateway redirect"
487508 ) ;
488- target = config. gateway . with_url ( peer_endpoint) ;
509+ // Prefer the replica the shared host moved to.
510+ let home = match gateway_home. as_mut ( ) {
511+ Some ( home) => wait_for_home ( home, Some ( target. url ( ) ) ) . await ,
512+ None => None ,
513+ } ;
514+ target = config. gateway . with_url ( home. unwrap_or ( peer_endpoint) ) ;
489515 redirected = true ;
516+ follow_home = false ;
490517 // No backoff: this is an expected handoff, not a failure.
491518 }
492519 Err ( e) => {
@@ -555,6 +582,22 @@ impl ReconnectBackoff {
555582 }
556583}
557584
585+ /// The peer endpoint of the replica serving the shared host, once it is one
586+ /// other than `avoid`, waiting up to [`GATEWAY_HOME_WAIT`].
587+ async fn wait_for_home (
588+ home : & mut watch:: Receiver < Option < String > > ,
589+ avoid : Option < & str > ,
590+ ) -> Option < String > {
591+ tokio:: time:: timeout (
592+ GATEWAY_HOME_WAIT ,
593+ home. wait_for ( |home| home. as_deref ( ) . is_some_and ( |home| Some ( home) != avoid) ) ,
594+ )
595+ . await
596+ . ok ( ) ?
597+ . ok ( )
598+ . and_then ( |home| ( * home) . clone ( ) )
599+ }
600+
558601/// Keep asking not to be redirected when a redirected attempt never got a
559602/// session, so a stale ring pointing at a dead replica cannot bounce us back
560603/// to it. A session that was accepted and later dropped starts placement over.
@@ -1636,6 +1679,31 @@ mod ocsf_event_tests {
16361679 assert ! ( !redirect_survives_failure( false , true ) ) ;
16371680 }
16381681
1682+ #[ tokio:: test( start_paused = true ) ]
1683+ async fn shared_host_sessions_wait_for_the_hosts_gateway ( ) {
1684+ let ( tx, mut home) = watch:: channel ( None ) ;
1685+ assert_eq ! ( wait_for_home( & mut home, None ) . await , None ) ;
1686+
1687+ tx. send_replace ( Some ( "https://gw-a:8443" . to_string ( ) ) ) ;
1688+ assert_eq ! (
1689+ wait_for_home( & mut home, None ) . await . as_deref( ) ,
1690+ Some ( "https://gw-a:8443" )
1691+ ) ;
1692+ assert_eq ! (
1693+ wait_for_home( & mut home, Some ( "https://gw-a:8443" ) ) . await ,
1694+ None
1695+ ) ;
1696+
1697+ let mut moving = home. clone ( ) ;
1698+ let moved =
1699+ tokio:: spawn (
1700+ async move { wait_for_home ( & mut moving, Some ( "https://gw-a:8443" ) ) . await } ,
1701+ ) ;
1702+ tokio:: task:: yield_now ( ) . await ;
1703+ tx. send_replace ( Some ( "https://gw-b:8443" . to_string ( ) ) ) ;
1704+ assert_eq ! ( moved. await . unwrap( ) . as_deref( ) , Some ( "https://gw-b:8443" ) ) ;
1705+ }
1706+
16391707 #[ test]
16401708 fn reconnect_backoff_doubles_until_a_session_is_accepted ( ) {
16411709 let mut backoff = ReconnectBackoff :: new ( ) ;
0 commit comments