control/controlclient: rename Auto cancel methods, add missing Lock variant

Then use the Locked variants in Shutdown while we already hold the lock.

Updates #cleanup

Change-Id: I367d53e6be6f37f783c8f43fc9c4d498d0adf501
Co-authored-by: Maisem Ali <maisem@tailscale.com>
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/9190/head
Brad Fitzpatrick 10 months ago committed by Brad Fitzpatrick
parent 61ae16cb6f
commit 86dc0af5ae

@ -231,7 +231,7 @@ func NewNoStart(opts Options) (_ *Auto, err error) {
func (c *Auto) SetPaused(paused bool) { func (c *Auto) SetPaused(paused bool) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if paused == c.paused { if paused == c.paused || c.closed {
return return
} }
c.logf("setPaused(%v)", paused) c.logf("setPaused(%v)", paused)
@ -239,7 +239,7 @@ func (c *Auto) SetPaused(paused bool) {
if paused { if paused {
// Only cancel the map routine. (The auth routine isn't expensive // Only cancel the map routine. (The auth routine isn't expensive
// so it's fine to keep it running.) // so it's fine to keep it running.)
c.cancelMapLocked() c.cancelMapCtxLocked()
} else { } else {
for _, ch := range c.unpauseWaiters { for _, ch := range c.unpauseWaiters {
close(ch) close(ch)
@ -279,9 +279,16 @@ func (c *Auto) updateControl() {
} }
} }
func (c *Auto) cancelAuth() { // cancelAuthCtx cancels the existing auth goroutine's context
// & creates a new one, causing it to restart.
func (c *Auto) cancelAuthCtx() {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
c.cancelAuthCtxLocked()
}
// cancelAuthCtxLocked is like cancelAuthCtx, but assumes the caller holds c.mu.
func (c *Auto) cancelAuthCtxLocked() {
if c.authCancel != nil { if c.authCancel != nil {
c.authCancel() c.authCancel()
} }
@ -291,8 +298,16 @@ func (c *Auto) cancelAuth() {
} }
} }
// cancelMapLocked is like cancelMap, but assumes the caller holds c.mu. // cancelMapCtx cancels the context for the existing mapPoll and liteUpdates
func (c *Auto) cancelMapLocked() { // goroutines and creates a new one, causing them to restart.
func (c *Auto) cancelMapCtx() {
c.mu.Lock()
defer c.mu.Unlock()
c.cancelMapCtxLocked()
}
// cancelMapCtxLocked is like cancelMapCtx, but assumes the caller holds c.mu.
func (c *Auto) cancelMapCtxLocked() {
if c.mapCancel != nil { if c.mapCancel != nil {
c.mapCancel() c.mapCancel()
} }
@ -302,18 +317,11 @@ func (c *Auto) cancelMapLocked() {
} }
} }
// cancelMap cancels the existing mapPoll and liteUpdates.
func (c *Auto) cancelMap() {
c.mu.Lock()
defer c.mu.Unlock()
c.cancelMapLocked()
}
// restartMap cancels the existing mapPoll and liteUpdates, and then starts a // restartMap cancels the existing mapPoll and liteUpdates, and then starts a
// new one. // new one.
func (c *Auto) restartMap() { func (c *Auto) restartMap() {
c.mu.Lock() c.mu.Lock()
c.cancelMapLocked() c.cancelMapCtxLocked()
synced := c.synced synced := c.synced
c.mu.Unlock() c.mu.Unlock()
@ -676,7 +684,7 @@ func (c *Auto) Login(t *tailcfg.Oauth2Token, flags LoginFlags) {
} }
c.mu.Unlock() c.mu.Unlock()
c.cancelAuth() c.cancelAuthCtx()
} }
func (c *Auto) Logout(ctx context.Context) error { func (c *Auto) Logout(ctx context.Context) error {
@ -690,8 +698,8 @@ func (c *Auto) Logout(ctx context.Context) error {
loggedOutResult: errc, loggedOutResult: errc,
} }
c.mu.Unlock() c.mu.Unlock()
c.cancelAuth() c.cancelAuthCtx()
c.cancelMap() c.cancelMapCtx()
timer, timerChannel := c.clock.NewTimer(10 * time.Second) timer, timerChannel := c.clock.NewTimer(10 * time.Second)
defer timer.Stop() defer timer.Stop()
@ -729,6 +737,8 @@ func (c *Auto) Shutdown() {
direct := c.direct direct := c.direct
if !closed { if !closed {
c.closed = true c.closed = true
c.cancelAuthCtxLocked()
c.cancelMapCtxLocked()
} }
c.mu.Unlock() c.mu.Unlock()
@ -736,9 +746,7 @@ func (c *Auto) Shutdown() {
if !closed { if !closed {
c.unregisterHealthWatch() c.unregisterHealthWatch()
close(c.quit) close(c.quit)
c.cancelAuth()
<-c.authDone <-c.authDone
c.cancelMap()
<-c.mapDone <-c.mapDone
<-c.updateDone <-c.updateDone
if direct != nil { if direct != nil {

Loading…
Cancel
Save