From 5c3db0dcf54c5388323252da617d170cef6a3ac2 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 3 Jan 2021 16:40:38 +0100 Subject: [PATCH] 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). --- dnscrypt-proxy/serversInfo.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/dnscrypt-proxy/serversInfo.go b/dnscrypt-proxy/serversInfo.go index 47f4d655..9dd1a76f 100644 --- a/dnscrypt-proxy/serversInfo.go +++ b/dnscrypt-proxy/serversInfo.go @@ -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 { serverIdx := -1 - for i, registeredServer := range proxy.registeredServers { + proxy.serversInfo.RLock() + for i, registeredServer := range proxy.serversInfo.registeredServers { if registeredServer.name == name { serverIdx = i break @@ -278,7 +279,8 @@ func findFarthestRoute(proxy *Proxy, name string, relayStamps []stamps.ServerSta if serverIdx < 0 { return nil } - server := proxy.registeredServers[serverIdx] + server := proxy.serversInfo.registeredServers[serverIdx] + proxy.serversInfo.RUnlock() serverAddrStr, _ := ExtractHostAndPort(server.stamp.ServerAddrStr, 443) serverAddr := net.ParseIP(serverAddrStr) if serverAddr == nil { @@ -350,12 +352,14 @@ func route(proxy *Proxy, name string) (*Relay, error) { break } } - for _, registeredServer := range proxy.registeredServers { + proxy.serversInfo.RLock() + for _, registeredServer := range proxy.serversInfo.registeredServers { if registeredServer.name == relayName { relayStamps = append(relayStamps, registeredServer.stamp) break } } + proxy.serversInfo.RUnlock() } } if len(relayStamps) == 0 {