88use std:: { collections:: BTreeMap , net:: SocketAddr } ;
99
1010use n0_future:: time:: Instant ;
11+ use tracing:: { event, Level } ;
1112
1213use super :: { path_state:: PathState , IpPort } ;
1314
@@ -24,7 +25,7 @@ use super::{path_state::PathState, IpPort};
2425///
2526/// [`MagicSock`]: crate::magicsock::MagicSock
2627/// [`NodeState`]: super::node_state::NodeState
27- #[ derive( Debug , Default , Clone , Copy ) ]
28+ #[ derive( Debug , Default , Clone , Copy , PartialEq , Eq ) ]
2829pub ( super ) enum UdpSendAddr {
2930 /// The UDP address can be relied on to deliver data to the remote node.
3031 ///
@@ -113,9 +114,34 @@ impl NodeUdpPaths {
113114 & self . best
114115 }
115116
116- pub ( super ) fn update_to_best_addr ( & mut self , now : Instant ) {
117- self . best_ipv4 = self . best_addr ( false , now) ;
118- self . best = self . best_addr ( true , now) ;
117+ /// Changes the current best address(es) to ones chosen as described in [`Self::best_addr`] docs.
118+ ///
119+ /// Returns whether one of the best addresses had to change.
120+ ///
121+ /// This should be called any time that `paths` is modified.
122+ pub ( super ) fn update_to_best_addr ( & mut self , now : Instant ) -> bool {
123+ let best_ipv4 = self . best_addr ( false , now) ;
124+ let best = self . best_addr ( true , now) ;
125+ let mut changed = false ;
126+ if best_ipv4 != self . best_ipv4 {
127+ event ! (
128+ target: "iroh::_events::udp::best_ipv4" ,
129+ Level :: DEBUG ,
130+ ?best_ipv4,
131+ ) ;
132+ changed = true ;
133+ }
134+ if best != self . best {
135+ event ! (
136+ target: "iroh::_events::udp::best" ,
137+ Level :: DEBUG ,
138+ ?best,
139+ ) ;
140+ changed = true ;
141+ }
142+ self . best_ipv4 = best_ipv4;
143+ self . best = best;
144+ changed
119145 }
120146
121147 pub ( super ) fn best_addr ( & self , have_ipv6 : bool , now : Instant ) -> UdpSendAddr {
0 commit comments