ipn/ipnlocal: use eventbus.SubscribeFunc in expiryManager

Updates #15160
Updates #17487

Change-Id: I8721e3ac1af505630edca7c5cb50695b0aad832a
Signed-off-by: M. J. Fromberger <fromberger@tailscale.com>
This commit is contained in:
M. J. Fromberger 2025-10-07 17:10:53 -07:00 committed by M. J. Fromberger
parent 241ea1c98b
commit 109cb50d5f

View File

@ -43,7 +43,7 @@ type expiryManager struct {
logf logger.Logf
clock tstime.Clock
eventSubs eventbus.Monitor
eventClient *eventbus.Client
}
func newExpiryManager(logf logger.Logf, bus *eventbus.Bus) *expiryManager {
@ -53,30 +53,13 @@ func newExpiryManager(logf logger.Logf, bus *eventbus.Bus) *expiryManager {
clock: tstime.StdClock{},
}
cli := bus.Client("ipnlocal.expiryManager")
em.eventSubs = cli.Monitor(em.consumeEventbusTopics(cli))
em.eventClient = bus.Client("ipnlocal.expiryManager")
eventbus.SubscribeFunc(em.eventClient, func(ct controlclient.ControlTime) {
em.onControlTime(ct.Value)
})
return em
}
// consumeEventbusTopics consumes events from all relevant
// [eventbus.Subscriber]'s and passes them to their related handler. Events are
// always handled in the order they are received, i.e. the next event is not
// read until the previous event's handler has returned. It returns when the
// [eventbus.Client] is closed.
func (em *expiryManager) consumeEventbusTopics(cli *eventbus.Client) func(*eventbus.Client) {
controlTimeSub := eventbus.Subscribe[controlclient.ControlTime](cli)
return func(cli *eventbus.Client) {
for {
select {
case <-cli.Done():
return
case time := <-controlTimeSub.Events():
em.onControlTime(time.Value)
}
}
}
}
// onControlTime is called whenever we receive a new timestamp from the control
// server to store the delta.
func (em *expiryManager) onControlTime(t time.Time) {
@ -245,7 +228,7 @@ func (em *expiryManager) nextPeerExpiry(nm *netmap.NetworkMap, localNow time.Tim
return nextExpiry
}
func (em *expiryManager) close() { em.eventSubs.Close() }
func (em *expiryManager) close() { em.eventClient.Close() }
// ControlNow estimates the current time on the control server, calculated as
// localNow + the delta between local and control server clocks as recorded