wgengine/magicsock: add a backoff on DERP reconnects

Fixes #808
reviewable/pr836/r1
Brad Fitzpatrick 4 years ago
parent 22024a38c3
commit ac866054c7

@ -40,6 +40,7 @@ import (
"tailscale.com/derp/derphttp" "tailscale.com/derp/derphttp"
"tailscale.com/disco" "tailscale.com/disco"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/logtail/backoff"
"tailscale.com/net/dnscache" "tailscale.com/net/dnscache"
"tailscale.com/net/interfaces" "tailscale.com/net/interfaces"
"tailscale.com/net/netcheck" "tailscale.com/net/netcheck"
@ -1340,7 +1341,7 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d
// peerPresent is the set of senders we know are present on this // peerPresent is the set of senders we know are present on this
// connection, based on messages we've received from the server. // connection, based on messages we've received from the server.
peerPresent := map[key.Public]bool{} peerPresent := map[key.Public]bool{}
bo := backoff.NewBackoff(fmt.Sprintf("derp-%d", regionID), c.logf, 5*time.Second)
for { for {
msg, err := dc.Recv() msg, err := dc.Recv()
if err != nil { if err != nil {
@ -1361,19 +1362,24 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d
return return
default: default:
} }
c.ReSTUN("derp-close")
c.logf("magicsock: [%p] derp.Recv(derp-%d): %v", dc, regionID, err) c.logf("magicsock: [%p] derp.Recv(derp-%d): %v", dc, regionID, err)
// Avoid excessive spinning. // If our DERP connection broke, it might be because our network
// TODO: use a backoff timer, perhaps between 10ms and 500ms? // conditions changed. Start that check.
// Don't want to sleep too long. For now 250ms seems fine. c.ReSTUN("derp-recv-error")
// Back off a bit before reconnecting.
bo.BackOff(ctx, err)
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-time.After(250 * time.Millisecond): default:
} }
continue continue
} }
bo.BackOff(ctx, nil) // reset
switch m := msg.(type) { switch m := msg.(type) {
case derp.ReceivedPacket: case derp.ReceivedPacket:
pkt = m pkt = m

Loading…
Cancel
Save