@@ -11,35 +11,30 @@ import (
1111//
1212// Detection lives here in the udp package, next to the socket it concerns and the platform matrix that already knows
1313// which sockets go stale. What to do about a change — updating the lighthouse, requerying tunnels — is not the udp
14- // package's business, so the reaction is injected as a plain function pointer. The monitor calls it once a change
15- // has settled and stays ignorant of what it does .
14+ // package's business, so Start takes the reaction as a plain function. Passing it at Start rather than holding it
15+ // keeps this package from referencing whatever owns the rebind .
1616//
1717// On platforms whose sockets do not go stale, watchNetworkChanges hands back a nil channel and Start returns.
1818type NetworkChangeMonitor struct {
1919 l * slog.Logger
2020 ctx context.Context
2121 enabled bool
22- // rebind is what we call once a change has settled. It is injected because the udp package has no business
23- // knowing what a rebind entails, only when one is warranted.
24- rebind func ()
2522}
2623
27- // NewNetworkChangeMonitor builds a monitor that calls rebind whenever the local network moves. The returned monitor
28- // is always usable: Start is safe to call unconditionally, it no-ops when disabled or on a platform that does not
29- // need it.
30- func NewNetworkChangeMonitor (ctx context.Context , l * slog.Logger , c * config.C , rebind func ()) * NetworkChangeMonitor {
24+ // NewNetworkChangeMonitor builds a monitor for local network changes. The returned monitor is always usable: Start
25+ // is safe to call unconditionally, it no-ops when disabled or on a platform that does not need it.
26+ func NewNetworkChangeMonitor (ctx context.Context , l * slog.Logger , c * config.C ) * NetworkChangeMonitor {
3127 return & NetworkChangeMonitor {
3228 l : l ,
3329 ctx : ctx ,
3430 enabled : c .GetBool ("listen.rebind_on_network_change" , true ),
35- rebind : rebind ,
3631 }
3732}
3833
3934// Start watches for network changes until the context is cancelled, calling rebind once per settled change. It
4035// blocks, so callers run it in a goroutine, and it no-ops when disabled, unsupported, or with nothing to rebind.
41- func (m * NetworkChangeMonitor ) Start () {
42- if ! m .enabled || m . rebind == nil || m .ctx .Err () != nil {
36+ func (m * NetworkChangeMonitor ) Start (rebind func () ) {
37+ if ! m .enabled || rebind == nil || m .ctx .Err () != nil {
4338 return
4439 }
4540
@@ -61,6 +56,6 @@ func (m *NetworkChangeMonitor) Start() {
6156
6257 for range changes {
6358 m .l .Info ("Local network changed, rebinding the udp listener" )
64- m . rebind ()
59+ rebind ()
6560 }
6661}
0 commit comments