mirror of
https://github.com/tailscale/tailscale.git
synced 2025-09-21 13:41:46 +02:00
controlclient/auto: switch eventbus to using a monitor
Only changes how the go routine consuming the events starts and stops, not what it does. Updates #15160 Signed-off-by: Claus Lensbøl <claus@tailscale.com>
This commit is contained in:
parent
ca9d795006
commit
ddc5df1f82
@ -123,9 +123,7 @@ type Auto struct {
|
|||||||
observerQueue execqueue.ExecQueue
|
observerQueue execqueue.ExecQueue
|
||||||
shutdownFn func() // to be called prior to shutdown or nil
|
shutdownFn func() // to be called prior to shutdown or nil
|
||||||
|
|
||||||
eventClient *eventbus.Client
|
eventSubs eventbus.Monitor
|
||||||
healthChangeSub *eventbus.Subscriber[health.Change]
|
|
||||||
subsDoneCh chan struct{} // close-only channel when eventClient has closed
|
|
||||||
|
|
||||||
mu sync.Mutex // mutex guards the following fields
|
mu sync.Mutex // mutex guards the following fields
|
||||||
|
|
||||||
@ -195,11 +193,11 @@ func NewNoStart(opts Options) (_ *Auto, err error) {
|
|||||||
updateDone: make(chan struct{}),
|
updateDone: make(chan struct{}),
|
||||||
observer: opts.Observer,
|
observer: opts.Observer,
|
||||||
shutdownFn: opts.Shutdown,
|
shutdownFn: opts.Shutdown,
|
||||||
subsDoneCh: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.eventClient = opts.Bus.Client("controlClient.Auto")
|
// Set up eventbus client and subscriber
|
||||||
c.healthChangeSub = eventbus.Subscribe[health.Change](c.eventClient)
|
cli := opts.Bus.Client("controlClient.Auto")
|
||||||
|
c.eventSubs = cli.Monitor(c.consumeEventbusTopics(cli))
|
||||||
|
|
||||||
c.authCtx, c.authCancel = context.WithCancel(context.Background())
|
c.authCtx, c.authCancel = context.WithCancel(context.Background())
|
||||||
c.authCtx = sockstats.WithSockStats(c.authCtx, sockstats.LabelControlClientAuto, opts.Logf)
|
c.authCtx = sockstats.WithSockStats(c.authCtx, sockstats.LabelControlClientAuto, opts.Logf)
|
||||||
@ -207,7 +205,6 @@ func NewNoStart(opts Options) (_ *Auto, err error) {
|
|||||||
c.mapCtx, c.mapCancel = context.WithCancel(context.Background())
|
c.mapCtx, c.mapCancel = context.WithCancel(context.Background())
|
||||||
c.mapCtx = sockstats.WithSockStats(c.mapCtx, sockstats.LabelControlClientAuto, opts.Logf)
|
c.mapCtx = sockstats.WithSockStats(c.mapCtx, sockstats.LabelControlClientAuto, opts.Logf)
|
||||||
|
|
||||||
go c.consumeEventbusTopics()
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,16 +213,17 @@ func NewNoStart(opts Options) (_ *Auto, err error) {
|
|||||||
// always handled in the order they are received, i.e. the next event is not
|
// 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
|
// read until the previous event's handler has returned. It returns when the
|
||||||
// [eventbus.Client] is closed.
|
// [eventbus.Client] is closed.
|
||||||
func (c *Auto) consumeEventbusTopics() {
|
func (c *Auto) consumeEventbusTopics(cli *eventbus.Client) func(*eventbus.Client) {
|
||||||
defer close(c.subsDoneCh)
|
healthChangeSub := eventbus.Subscribe[health.Change](cli)
|
||||||
|
return func(cli *eventbus.Client) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-c.eventClient.Done():
|
case <-cli.Done():
|
||||||
return
|
return
|
||||||
case change := <-c.healthChangeSub.Events():
|
case change := <-healthChangeSub.Events():
|
||||||
if change.WarnableChanged {
|
if change.WarnableChanged {
|
||||||
c.direct.ReportWarnableChange(change.Warnable, change.UnhealthyState)
|
c.direct.ReportWarnableChange(change.Warnable, change.UnhealthyState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -784,8 +782,7 @@ func (c *Auto) UpdateEndpoints(endpoints []tailcfg.Endpoint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Auto) Shutdown() {
|
func (c *Auto) Shutdown() {
|
||||||
c.eventClient.Close()
|
c.eventSubs.Close()
|
||||||
<-c.subsDoneCh
|
|
||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
if c.closed {
|
if c.closed {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user