ipn, wgengine/magicsock: be more idle when in Stopped state with no peers

(Previously as #288, but with some more.)

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/335/head
Brad Fitzpatrick 5 years ago
parent 66c7875974
commit 18017f7630

@ -698,6 +698,7 @@ func (b *LocalBackend) enterState(newState State) {
state := b.state state := b.state
prefs := b.prefs prefs := b.prefs
notify := b.notify notify := b.notify
c := b.c
b.mu.Unlock() b.mu.Unlock()
if state == newState { if state == newState {
@ -719,6 +720,9 @@ func (b *LocalBackend) enterState(newState State) {
if err != nil { if err != nil {
b.logf("Reconfig(down): %v", err) b.logf("Reconfig(down): %v", err)
} }
if c != nil {
c.Shutdown()
}
case Starting, NeedsMachineAuth: case Starting, NeedsMachineAuth:
b.authReconfig() b.authReconfig()
// Needed so that UpdateEndpoints can run // Needed so that UpdateEndpoints can run

@ -1230,6 +1230,7 @@ func (c *Conn) SetPrivateKey(privateKey wgcfg.PrivateKey) error {
if oldKey.IsZero() { if oldKey.IsZero() {
c.logf("magicsock: SetPrivateKey called (init)") c.logf("magicsock: SetPrivateKey called (init)")
go c.ReSTUN("set-private-key")
} else { } else {
c.logf("magicsock: SetPrivateKey called (changed") c.logf("magicsock: SetPrivateKey called (changed")
} }
@ -1264,6 +1265,10 @@ func (c *Conn) UpdatePeers(newPeers map[key.Public]struct{}) {
delete(c.peerLastDerp, peer) delete(c.peerLastDerp, peer)
} }
} }
if len(oldPeers) == 0 && len(newPeers) > 0 {
go c.ReSTUN("non-zero-peers")
}
} }
// SetDERPEnabled controls whether DERP is used. // SetDERPEnabled controls whether DERP is used.
@ -1410,6 +1415,12 @@ func (c *Conn) Close() error {
return err return err
} }
func (c *Conn) haveAnyPeers() bool {
c.mu.Lock()
defer c.mu.Unlock()
return len(c.peerSet) > 0
}
func (c *Conn) periodicReSTUN() { func (c *Conn) periodicReSTUN() {
prand := rand.New(rand.NewSource(time.Now().UnixNano())) prand := rand.New(rand.NewSource(time.Now().UnixNano()))
dur := func() time.Duration { dur := func() time.Duration {
@ -1423,7 +1434,9 @@ func (c *Conn) periodicReSTUN() {
case <-c.donec(): case <-c.donec():
return return
case <-timer.C: case <-timer.C:
if c.haveAnyPeers() {
c.ReSTUN("periodic") c.ReSTUN("periodic")
}
timer.Reset(dur()) timer.Reset(dur())
} }
} }

Loading…
Cancel
Save