Try to rely on proxy.serversInfo rather than proxy.registeredServers

With the introduction of background updates, I'm a little bit worried
about race conditions that can happen when a new server or relay is
registered (or even when a stamp changes).
This commit is contained in:
Frank Denis 2021-01-03 16:40:38 +01:00
parent fbd598f027
commit 5c3db0dcf5
1 changed files with 7 additions and 3 deletions

View File

@ -269,7 +269,8 @@ func fetchServerInfo(proxy *Proxy, name string, stamp stamps.ServerStamp, isNew
func findFarthestRoute(proxy *Proxy, name string, relayStamps []stamps.ServerStamp) *stamps.ServerStamp { func findFarthestRoute(proxy *Proxy, name string, relayStamps []stamps.ServerStamp) *stamps.ServerStamp {
serverIdx := -1 serverIdx := -1
for i, registeredServer := range proxy.registeredServers { proxy.serversInfo.RLock()
for i, registeredServer := range proxy.serversInfo.registeredServers {
if registeredServer.name == name { if registeredServer.name == name {
serverIdx = i serverIdx = i
break break
@ -278,7 +279,8 @@ func findFarthestRoute(proxy *Proxy, name string, relayStamps []stamps.ServerSta
if serverIdx < 0 { if serverIdx < 0 {
return nil return nil
} }
server := proxy.registeredServers[serverIdx] server := proxy.serversInfo.registeredServers[serverIdx]
proxy.serversInfo.RUnlock()
serverAddrStr, _ := ExtractHostAndPort(server.stamp.ServerAddrStr, 443) serverAddrStr, _ := ExtractHostAndPort(server.stamp.ServerAddrStr, 443)
serverAddr := net.ParseIP(serverAddrStr) serverAddr := net.ParseIP(serverAddrStr)
if serverAddr == nil { if serverAddr == nil {
@ -350,12 +352,14 @@ func route(proxy *Proxy, name string) (*Relay, error) {
break break
} }
} }
for _, registeredServer := range proxy.registeredServers { proxy.serversInfo.RLock()
for _, registeredServer := range proxy.serversInfo.registeredServers {
if registeredServer.name == relayName { if registeredServer.name == relayName {
relayStamps = append(relayStamps, registeredServer.stamp) relayStamps = append(relayStamps, registeredServer.stamp)
break break
} }
} }
proxy.serversInfo.RUnlock()
} }
} }
if len(relayStamps) == 0 { if len(relayStamps) == 0 {