wgengine/magicsock: remove a per-DERP-packet map lookup in common case

Updates #150

Change-Id: Iffb6eccbe7ca97af97d29be63b7e37d487b3ba28
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/3735/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 5a317d312d
commit 5404a0557b

@ -1508,6 +1508,7 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d
peerPresent := map[key.NodePublic]bool{} peerPresent := map[key.NodePublic]bool{}
bo := backoff.NewBackoff(fmt.Sprintf("derp-%d", regionID), c.logf, 5*time.Second) bo := backoff.NewBackoff(fmt.Sprintf("derp-%d", regionID), c.logf, 5*time.Second)
var lastPacketTime time.Time var lastPacketTime time.Time
var lastPacketSrc key.NodePublic
for { for {
msg, connGen, err := dc.RecvDetail() msg, connGen, err := dc.RecvDetail()
@ -1569,9 +1570,12 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d
} }
// If this is a new sender we hadn't seen before, remember it and // If this is a new sender we hadn't seen before, remember it and
// register a route for this peer. // register a route for this peer.
if _, ok := peerPresent[res.src]; !ok { if res.src != lastPacketSrc { // avoid map lookup w/ high throughput single peer
peerPresent[res.src] = true lastPacketSrc = res.src
c.addDerpPeerRoute(res.src, regionID, dc) if _, ok := peerPresent[res.src]; !ok {
peerPresent[res.src] = true
c.addDerpPeerRoute(res.src, regionID, dc)
}
} }
case derp.PingMessage: case derp.PingMessage:
// Best effort reply to the ping. // Best effort reply to the ping.

Loading…
Cancel
Save