Kaynağa Gözat

Unlock the hostmap quickly, lock hostinfo instead (#459)

Nate Brown 4 yıl önce
ebeveyn
işleme
d004fae4f9
1 değiştirilmiş dosya ile 8 ekleme ve 11 silme
  1. 8 11
      hostmap.go

+ 8 - 11
hostmap.go

@@ -313,24 +313,17 @@ func (hm *HostMap) PromoteBestQueryVpnIP(vpnIp uint32, ifce *Interface) (*HostIn
 func (hm *HostMap) queryVpnIP(vpnIp uint32, promoteIfce *Interface) (*HostInfo, error) {
 	hm.RLock()
 	if h, ok := hm.Hosts[vpnIp]; ok {
+		hm.RUnlock()
 		// Do not attempt promotion if you are a lighthouse
 		if promoteIfce != nil && !promoteIfce.lightHouse.amLighthouse {
 			h.TryPromoteBest(hm.preferredRanges, promoteIfce)
 		}
-		hm.RUnlock()
 		return h, nil
 
-	} else {
-		//return &net.UDPAddr{}, nil, errors.New("Unable to find host")
-		hm.RUnlock()
-		/*
-			if lightHouse != nil {
-				lightHouse.Query(vpnIp)
-				return nil, errors.New("Unable to find host")
-			}
-		*/
-		return nil, errors.New("unable to find host")
 	}
+
+	hm.RUnlock()
+	return nil, errors.New("unable to find host")
 }
 
 func (hm *HostMap) queryUnsafeRoute(ip uint32) uint32 {
@@ -415,6 +408,10 @@ func (i *HostInfo) BindConnectionState(cs *ConnectionState) {
 func (i *HostInfo) TryPromoteBest(preferredRanges []*net.IPNet, ifce *Interface) {
 	c := atomic.AddUint32(&i.promoteCounter, 1)
 	if c%PromoteEvery == 0 {
+		// The lock here is currently protecting i.remote access
+		i.RLock()
+		defer i.RUnlock()
+
 		// return early if we are already on a preferred remote
 		rIP := i.remote.IP
 		for _, l := range preferredRanges {