Threadsafe update (#1579)

* threadsafe update for relays

* locks around registeredRelays
This commit is contained in:
Ian Bashford 2021-01-09 21:44:32 +00:00 committed by GitHub
parent 85d268f2b9
commit fcd9225121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 6 deletions

View File

@ -352,6 +352,9 @@ func (proxy *Proxy) updateRegisteredServers() error {
for _, registeredServer := range proxy.registeredServers { for _, registeredServer := range proxy.registeredServers {
proxy.serversInfo.registerServer(registeredServer.name, registeredServer.stamp) proxy.serversInfo.registerServer(registeredServer.name, registeredServer.stamp)
} }
for _, registeredRelay := range proxy.registeredRelays {
proxy.serversInfo.registerRelay(registeredRelay.name, registeredRelay.stamp)
}
return nil return nil
} }

View File

@ -116,12 +116,13 @@ type ServersInfo struct {
sync.RWMutex sync.RWMutex
inner []*ServerInfo inner []*ServerInfo
registeredServers []RegisteredServer registeredServers []RegisteredServer
registeredRelays []RegisteredServer
lbStrategy LBStrategy lbStrategy LBStrategy
lbEstimator bool lbEstimator bool
} }
func NewServersInfo() ServersInfo { func NewServersInfo() ServersInfo {
return ServersInfo{lbStrategy: DefaultLBStrategy, lbEstimator: true, registeredServers: make([]RegisteredServer, 0)} return ServersInfo{lbStrategy: DefaultLBStrategy, lbEstimator: true, registeredServers: make([]RegisteredServer, 0), registeredRelays: make([]RegisteredServer, 0)}
} }
func (serversInfo *ServersInfo) registerServer(name string, stamp stamps.ServerStamp) { func (serversInfo *ServersInfo) registerServer(name string, stamp stamps.ServerStamp) {
@ -137,6 +138,19 @@ func (serversInfo *ServersInfo) registerServer(name string, stamp stamps.ServerS
serversInfo.registeredServers = append(serversInfo.registeredServers, newRegisteredServer) serversInfo.registeredServers = append(serversInfo.registeredServers, newRegisteredServer)
} }
func (serversInfo *ServersInfo) registerRelay(name string, stamp stamps.ServerStamp) {
newRegisteredServer := RegisteredServer{name: name, stamp: stamp}
serversInfo.Lock()
defer serversInfo.Unlock()
for i, oldRegisteredServer := range serversInfo.registeredRelays {
if oldRegisteredServer.name == name {
serversInfo.registeredRelays[i] = newRegisteredServer
return
}
}
serversInfo.registeredRelays = append(serversInfo.registeredRelays, newRegisteredServer)
}
func (serversInfo *ServersInfo) refreshServer(proxy *Proxy, name string, stamp stamps.ServerStamp) error { func (serversInfo *ServersInfo) refreshServer(proxy *Proxy, name string, stamp stamps.ServerStamp) error {
serversInfo.RLock() serversInfo.RLock()
isNew := true isNew := true
@ -286,7 +300,7 @@ func findFarthestRoute(proxy *Proxy, name string, relayStamps []stamps.ServerSta
if serverAddr == nil { if serverAddr == nil {
return nil return nil
} }
if len(proxy.registeredRelays) == 0 { if len(proxy.serversInfo.registeredRelays) == 0 {
return nil return nil
} }
bestRelayIdxs := make([]int, 0) bestRelayIdxs := make([]int, 0)
@ -340,19 +354,21 @@ func route(proxy *Proxy, name string) (*Relay, error) {
if relayStamp, err := stamps.NewServerStampFromString(relayName); err == nil { if relayStamp, err := stamps.NewServerStampFromString(relayName); err == nil {
relayStamps = append(relayStamps, relayStamp) relayStamps = append(relayStamps, relayStamp)
} else if relayName == "*" { } else if relayName == "*" {
for _, registeredServer := range proxy.registeredRelays { proxy.serversInfo.RLock()
for _, registeredServer := range proxy.serversInfo.registeredRelays {
relayStamps = append(relayStamps, registeredServer.stamp) relayStamps = append(relayStamps, registeredServer.stamp)
} }
proxy.serversInfo.RUnlock()
wildcard = true wildcard = true
break break
} else { } else {
for _, registeredServer := range proxy.registeredRelays { proxy.serversInfo.RLock()
for _, registeredServer := range proxy.serversInfo.registeredRelays {
if registeredServer.name == relayName { if registeredServer.name == relayName {
relayStamps = append(relayStamps, registeredServer.stamp) relayStamps = append(relayStamps, registeredServer.stamp)
break break
} }
} }
proxy.serversInfo.RLock()
for _, registeredServer := range proxy.serversInfo.registeredServers { for _, registeredServer := range proxy.serversInfo.registeredServers {
if registeredServer.name == relayName { if registeredServer.name == relayName {
relayStamps = append(relayStamps, registeredServer.stamp) relayStamps = append(relayStamps, registeredServer.stamp)
@ -375,12 +391,14 @@ func route(proxy *Proxy, name string) (*Relay, error) {
return nil, fmt.Errorf("No valid relay for server [%v]", name) return nil, fmt.Errorf("No valid relay for server [%v]", name)
} }
relayName := relayCandidateStamp.ServerAddrStr relayName := relayCandidateStamp.ServerAddrStr
for _, registeredServer := range proxy.registeredRelays { proxy.serversInfo.RLock()
for _, registeredServer := range proxy.serversInfo.registeredRelays {
if registeredServer.stamp.ServerAddrStr == relayCandidateStamp.ServerAddrStr { if registeredServer.stamp.ServerAddrStr == relayCandidateStamp.ServerAddrStr {
relayName = registeredServer.name relayName = registeredServer.name
break break
} }
} }
proxy.serversInfo.RUnlock()
switch relayCandidateStamp.Proto { switch relayCandidateStamp.Proto {
case stamps.StampProtoTypeDNSCrypt, stamps.StampProtoTypeDNSCryptRelay: case stamps.StampProtoTypeDNSCrypt, stamps.StampProtoTypeDNSCryptRelay:
relayUDPAddr, err := net.ResolveUDPAddr("udp", relayCandidateStamp.ServerAddrStr) relayUDPAddr, err := net.ResolveUDPAddr("udp", relayCandidateStamp.ServerAddrStr)