wgengine/magicsock: return early, outdent in derpWriteChanOfAddr

reviewable/pr209/r2
Brad Fitzpatrick 5 years ago
parent 8c4cef60f8
commit b6f77cc48d

@ -688,6 +688,8 @@ func (c *Conn) derpWriteChanOfAddr(addr *net.UDPAddr) chan<- derpWriteRequest {
if !addr.IP.Equal(derpMagicIP) { if !addr.IP.Equal(derpMagicIP) {
return nil return nil
} }
nodeID := addr.Port
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if !c.wantDerp || c.closed { if !c.wantDerp || c.closed {
@ -697,13 +699,18 @@ func (c *Conn) derpWriteChanOfAddr(addr *net.UDPAddr) chan<- derpWriteRequest {
c.logf("DERP lookup of %v with no private key; ignoring", addr) c.logf("DERP lookup of %v with no private key; ignoring", addr)
return nil return nil
} }
ad, ok := c.activeDerp[addr.Port]
if !ok { ad, ok := c.activeDerp[nodeID]
if ok {
*ad.lastWrite = time.Now()
return ad.writeCh
}
if c.activeDerp == nil { if c.activeDerp == nil {
c.activeDerp = make(map[int]activeDerp) c.activeDerp = make(map[int]activeDerp)
c.prevDerp = make(map[int]*syncs.WaitGroupChan) c.prevDerp = make(map[int]*syncs.WaitGroupChan)
} }
derpSrv := c.derps.ServerByID(addr.Port) derpSrv := c.derps.ServerByID(nodeID)
if derpSrv == nil || derpSrv.HostHTTPS == "" { if derpSrv == nil || derpSrv.HostHTTPS == "" {
return nil return nil
} }
@ -712,10 +719,10 @@ func (c *Conn) derpWriteChanOfAddr(addr *net.UDPAddr) chan<- derpWriteRequest {
// so it is safe to do under the mu lock. // so it is safe to do under the mu lock.
dc, err := derphttp.NewClient(c.privateKey, "https://"+derpSrv.HostHTTPS+"/derp", c.logf) dc, err := derphttp.NewClient(c.privateKey, "https://"+derpSrv.HostHTTPS+"/derp", c.logf)
if err != nil { if err != nil {
c.logf("derphttp.NewClient: port %d, host %q invalid? err: %v", addr.Port, derpSrv.HostHTTPS, err) c.logf("derphttp.NewClient: port %d, host %q invalid? err: %v", nodeID, derpSrv.HostHTTPS, err)
return nil return nil
} }
dc.NotePreferred(c.myDerp == addr.Port) dc.NotePreferred(c.myDerp == nodeID)
dc.DNSCache = dnscache.Get() dc.DNSCache = dnscache.Get()
dc.TLSConfig = c.derpTLSConfig dc.TLSConfig = c.derpTLSConfig
@ -726,23 +733,23 @@ func (c *Conn) derpWriteChanOfAddr(addr *net.UDPAddr) chan<- derpWriteRequest {
ad.writeCh = ch ad.writeCh = ch
ad.cancel = cancel ad.cancel = cancel
ad.lastWrite = new(time.Time) ad.lastWrite = new(time.Time)
c.activeDerp[addr.Port] = ad c.activeDerp[nodeID] = ad
// Build a startGate for the derp reader+writer // Build a startGate for the derp reader+writer
// goroutines, so they don't start running until any // goroutines, so they don't start running until any
// previous generation is closed. // previous generation is closed.
startGate := syncs.ClosedChan() startGate := syncs.ClosedChan()
if prev := c.prevDerp[addr.Port]; prev != nil { if prev := c.prevDerp[nodeID]; prev != nil {
startGate = prev.DoneChan() startGate = prev.DoneChan()
} }
// And register a WaitGroup(Chan) for this generation. // And register a WaitGroup(Chan) for this generation.
wg := syncs.NewWaitGroupChan() wg := syncs.NewWaitGroupChan()
wg.Add(2) wg.Add(2)
c.prevDerp[addr.Port] = wg c.prevDerp[nodeID] = wg
go c.runDerpReader(ctx, addr, dc, wg, startGate) go c.runDerpReader(ctx, addr, dc, wg, startGate)
go c.runDerpWriter(ctx, addr, dc, ch, wg, startGate) go c.runDerpWriter(ctx, addr, dc, ch, wg, startGate)
}
*ad.lastWrite = time.Now() *ad.lastWrite = time.Now()
return ad.writeCh return ad.writeCh
} }

Loading…
Cancel
Save