@@ -8,8 +8,10 @@ use std::time::Duration;
88pub ( crate ) enum ServiceChange {
99 /// Service removed from config entirely.
1010 Removed { name : String } ,
11- /// Service dependencies changed in config.
12- DepsChanged { name : String } ,
11+ /// Service's `proxy_dependencies` changed in config.
12+ ProxyDepsChanged { name : String } ,
13+ /// Service's `triggers` changed in config.
14+ TriggersChanged { name : String } ,
1315 /// Service entry-point timeout toggled in config (Some ↔ None).
1416 ReachabilityChanged { name : String } ,
1517 /// All replicas on a specific IP were removed (node disconnected).
@@ -70,11 +72,13 @@ pub(crate) fn detect_config_changes(
7072 // services with changed deps, reachability, or timeout
7173 for ( name, loaded_info) in loaded {
7274 if let Some ( old_info) = current. get ( name) {
73- if loaded_info. proxy_deps ( ) != old_info. proxy_deps ( )
74- || loaded_info. triggers ( ) != old_info. triggers ( )
75- {
76- changes. push ( ServiceChange :: DepsChanged { name : name. clone ( ) } ) ;
77- } else if loaded_info. timeout ( ) . is_some ( ) != old_info. timeout ( ) . is_some ( ) {
75+ if loaded_info. proxy_deps ( ) != old_info. proxy_deps ( ) {
76+ changes. push ( ServiceChange :: ProxyDepsChanged { name : name. clone ( ) } ) ;
77+ }
78+ if loaded_info. triggers ( ) != old_info. triggers ( ) {
79+ changes. push ( ServiceChange :: TriggersChanged { name : name. clone ( ) } ) ;
80+ }
81+ if loaded_info. timeout ( ) . is_some ( ) != old_info. timeout ( ) . is_some ( ) {
7882 // reachability toggled (Some <-> None)
7983 changes. push ( ServiceChange :: ReachabilityChanged { name : name. clone ( ) } ) ;
8084 } else if let ( Some ( new_timeout) , Some ( old_timeout) ) =
@@ -199,6 +203,15 @@ async fn teardown_invalidated_service(
199203
200204 for name in services_to_cleanup {
201205 teardown_chain ( & name, services, orchestrator, ProxyFilter :: All ) . await ;
206+ // For the invalidated service itself, tear down all of its backend
207+ // chains; for dependents, only chains that route through the
208+ // invalidated service.
209+ let only_through = if name == invalidated_service {
210+ None
211+ } else {
212+ Some ( invalidated_service)
213+ } ;
214+ teardown_all_backend_chains_for ( & name, only_through, services, orchestrator) . await ;
202215 }
203216
204217 if is_failed && let Some ( si @ ServiceInfo :: Registered ( _) ) = services. get ( invalidated_service) {
@@ -341,6 +354,107 @@ pub(crate) fn collect_dep_chain_edges(
341354 edges
342355}
343356
357+ /// Walk the backend trigger chains starting from a specific initiator replica
358+ /// and collect the `(client, dep_service_name)` edges. One chain per trigger
359+ /// port; each is walked as a linear chain. If `only_through` is `Some(dep)`,
360+ /// chains that don't reference `dep` are skipped — useful for dep-side teardown
361+ /// where only chains that go through the affected dep should come down.
362+ fn collect_backend_chain_edges (
363+ initiator_name : & str ,
364+ initiator_ip : IpAddr ,
365+ initiator_docker : Option < & str > ,
366+ only_through : Option < & str > ,
367+ services : & HashMap < String , ServiceInfo > ,
368+ ) -> Vec < ( Client , String ) > {
369+ let mut edges = Vec :: new ( ) ;
370+ let Some ( triggers) = services. get ( initiator_name) . map ( ServiceInfo :: triggers) else {
371+ return edges;
372+ } ;
373+ for chain in triggers. values ( ) {
374+ if let Some ( dep) = only_through
375+ && !chain. iter ( ) . any ( |d| d == dep)
376+ {
377+ continue ;
378+ }
379+ let mut current_name = initiator_name. to_string ( ) ;
380+ let mut current_ip = initiator_ip;
381+ let mut current_docker: Option < String > = initiator_docker. map ( String :: from) ;
382+ for dep_name in chain {
383+ let hop = emit_edge_and_probe_hop (
384+ & mut edges,
385+ & current_name,
386+ current_ip,
387+ current_docker. as_deref ( ) ,
388+ dep_name,
389+ services,
390+ ) ;
391+ match hop {
392+ Some ( ( ip, docker) ) => {
393+ current_name. clone_from ( dep_name) ;
394+ current_ip = ip;
395+ current_docker = docker;
396+ }
397+ None => break ,
398+ }
399+ }
400+ }
401+ edges
402+ }
403+
404+ /// Backend twin of `teardown_dep_chain`: walks the initiator's trigger chains
405+ /// and decrements each edge. `only_through` filters to chains containing the
406+ /// given dep name; `None` walks every chain.
407+ async fn teardown_backend_chain (
408+ initiator_name : & str ,
409+ initiator_ip : IpAddr ,
410+ initiator_docker : Option < & str > ,
411+ only_through : Option < & str > ,
412+ services : & mut HashMap < String , ServiceInfo > ,
413+ orchestrator : & Orchestrator ,
414+ ) {
415+ let edges = collect_backend_chain_edges (
416+ initiator_name,
417+ initiator_ip,
418+ initiator_docker,
419+ only_through,
420+ services,
421+ ) ;
422+ for ( client, dep_name) in edges {
423+ if let Some ( ServiceInfo :: Registered ( dep_reg) ) = services. get_mut ( & dep_name) {
424+ dep_reg. decrement_chain ( & client, orchestrator) . await ;
425+ }
426+ }
427+ }
428+
429+ /// Tear down every backend chain initiated by any replica of `initiator_name`.
430+ /// `only_through` filters to chains containing the given dep name.
431+ async fn teardown_all_backend_chains_for (
432+ initiator_name : & str ,
433+ only_through : Option < & str > ,
434+ services : & mut HashMap < String , ServiceInfo > ,
435+ orchestrator : & Orchestrator ,
436+ ) {
437+ let replicas: Vec < ( IpAddr , Option < String > ) > = match services. get ( initiator_name) {
438+ Some ( ServiceInfo :: Registered ( reg) ) => reg
439+ . replicas ( )
440+ . iter ( )
441+ . map ( |r| ( r. ip ( ) , r. docker_container ( ) . map ( String :: from) ) )
442+ . collect ( ) ,
443+ _ => vec ! [ ] ,
444+ } ;
445+ for ( ip, docker) in replicas {
446+ teardown_backend_chain (
447+ initiator_name,
448+ ip,
449+ docker. as_deref ( ) ,
450+ only_through,
451+ services,
452+ orchestrator,
453+ )
454+ . await ;
455+ }
456+ }
457+
344458/// Push a `(current → dep)` edge onto `edges` and return the dep replica's
345459/// `(ip, docker)` if a client for `current` is already set up there.
346460fn emit_edge_and_probe_hop (
@@ -417,6 +531,18 @@ async fn teardown_partial_replicas(
417531 ProxyFilter :: OnReplica ( src_ip, src_docker) ,
418532 )
419533 . await ;
534+ // Backend twin: tear down chains initiated by this upstream replica
535+ // that route through the affected dep, so deeper edges keyed by the
536+ // gone replica don't leak.
537+ teardown_backend_chain (
538+ client. name ( ) ,
539+ src_ip,
540+ src_docker,
541+ Some ( name) ,
542+ services,
543+ orchestrator,
544+ )
545+ . await ;
420546 }
421547 }
422548
@@ -438,11 +564,15 @@ pub(crate) async fn apply_changes(
438564 teardown_invalidated_service ( & name, true , services, orchestrator) . await ;
439565 services. remove ( & name) ;
440566 }
441- ServiceChange :: DepsChanged { name } => {
567+ ServiceChange :: ProxyDepsChanged { name } => {
442568 teardown_invalidated_service ( & name, false , services, orchestrator) . await ;
443569 }
570+ ServiceChange :: TriggersChanged { name } => {
571+ teardown_all_backend_chains_for ( & name, None , services, orchestrator) . await ;
572+ }
444573 ServiceChange :: ReachabilityChanged { name } => {
445574 teardown_chain ( & name, services, orchestrator, ProxyFilter :: All ) . await ;
575+ teardown_all_backend_chains_for ( & name, None , services, orchestrator) . await ;
446576 }
447577 ServiceChange :: ReplicasRemoved { name, ip } => {
448578 let is_last = if let Some ( ServiceInfo :: Registered ( reg) ) = services. get ( & name) {
@@ -455,6 +585,28 @@ pub(crate) async fn apply_changes(
455585 // Last replica gone — config-based cascade to transitive dependents
456586 teardown_invalidated_service ( & name, true , services, orchestrator) . await ;
457587 } else {
588+ // Backend chains initiated by replicas of `name` at `ip`
589+ let dockers: Vec < Option < String > > =
590+ if let Some ( ServiceInfo :: Registered ( reg) ) = services. get ( & name) {
591+ reg. replicas ( )
592+ . iter ( )
593+ . filter ( |r| r. ip ( ) == ip)
594+ . map ( |r| r. docker_container ( ) . map ( String :: from) )
595+ . collect ( )
596+ } else {
597+ vec ! [ ]
598+ } ;
599+ for docker in dockers {
600+ teardown_backend_chain (
601+ & name,
602+ ip,
603+ docker. as_deref ( ) ,
604+ None ,
605+ services,
606+ orchestrator,
607+ )
608+ . await ;
609+ }
458610 teardown_partial_replicas ( & name, ProxyFilter :: OnIp ( ip) , services, orchestrator)
459611 . await ;
460612 if let Some ( si) = services. get_mut ( & name) {
@@ -476,6 +628,16 @@ pub(crate) async fn apply_changes(
476628 if is_last {
477629 teardown_invalidated_service ( & name, true , services, orchestrator) . await ;
478630 } else {
631+ // Backend chains initiated by this specific replica
632+ teardown_backend_chain (
633+ & name,
634+ ip,
635+ docker_container. as_deref ( ) ,
636+ None ,
637+ services,
638+ orchestrator,
639+ )
640+ . await ;
479641 teardown_partial_replicas (
480642 & name,
481643 ProxyFilter :: OnReplica ( ip, docker_container. as_deref ( ) ) ,
0 commit comments