@ -41,32 +41,20 @@ import (
// preferredDERPFrameTime, so update with care.
// preferredDERPFrameTime, so update with care.
const frameReceiveRecordRate = 5 * time . Second
const frameReceiveRecordRate = 5 * time . Second
// useDerpRoute reports whether magicsock should enable the DERP
// return path optimization (Issue 150).
//
// By default it's enabled, unless an environment variable
// or control says to disable it.
func ( c * Conn ) useDerpRoute ( ) bool {
if b , ok := debugUseDerpRoute ( ) . Get ( ) ; ok {
return b
}
return c . controlKnobs == nil || ! c . controlKnobs . DisableDRPO . Load ( )
}
// derpRoute is a route entry for a public key, saying that a certain
// derpRoute is a route entry for a public key, saying that a certain
// peer should be available at DERP node derp ID, as long as the
// peer should be available at DERP regionID, as long as the
// current connection for that derp ID is dc. (but dc should not be
// current connection for that regionID is dc. (but dc should not be
// used to write directly; it's owned by the read/write loops)
// used to write directly; it's owned by the read/write loops)
type derpRoute struct {
type derpRoute struct {
derp ID int
regionID int
dc * derphttp . Client // don't use directly; see comment above
dc * derphttp . Client // don't use directly; see comment above
}
}
// removeDerpPeerRoute removes a DERP route entry previously added by addDerpPeerRoute.
// removeDerpPeerRoute removes a DERP route entry previously added by addDerpPeerRoute.
func ( c * Conn ) removeDerpPeerRoute ( peer key . NodePublic , derp ID int , dc * derphttp . Client ) {
func ( c * Conn ) removeDerpPeerRoute ( peer key . NodePublic , regionID int , dc * derphttp . Client ) {
c . mu . Lock ( )
c . mu . Lock ( )
defer c . mu . Unlock ( )
defer c . mu . Unlock ( )
r2 := derpRoute { derp ID, dc }
r2 := derpRoute { region ID, dc }
if r , ok := c . derpRoute [ peer ] ; ok && r == r2 {
if r , ok := c . derpRoute [ peer ] ; ok && r == r2 {
delete ( c . derpRoute , peer )
delete ( c . derpRoute , peer )
}
}
@ -365,10 +353,10 @@ func (c *Conn) derpWriteChanForRegion(regionID int, peer key.NodePublic) chan<-
// perhaps peer's home is Frankfurt, but they dialed our home DERP
// perhaps peer's home is Frankfurt, but they dialed our home DERP
// node in SF to reach us, so we can reply to them using our
// node in SF to reach us, so we can reply to them using our
// SF connection rather than dialing Frankfurt. (Issue 150)
// SF connection rather than dialing Frankfurt. (Issue 150)
if ! peer . IsZero ( ) && c . useDerpRoute ( ) {
if ! peer . IsZero ( ) {
if r , ok := c . derpRoute [ peer ] ; ok {
if r , ok := c . derpRoute [ peer ] ; ok {
if ad , ok := c . activeDerp [ r . derp ID] ; ok && ad . c == r . dc {
if ad , ok := c . activeDerp [ r . region ID] ; ok && ad . c == r . dc {
c . setPeerLastDerpLocked ( peer , r . derp ID, regionID )
c . setPeerLastDerpLocked ( peer , r . region ID, regionID )
* ad . lastWrite = time . Now ( )
* ad . lastWrite = time . Now ( )
return ad . writeCh
return ad . writeCh
}
}