cleanup: serversInfo: simplify handling onld/new servers

This commit is contained in:
Markus Linnala 2019-10-18 19:57:04 +03:00 committed by Frank Denis
parent 63520e494d
commit 683b111bf9
1 changed files with 7 additions and 8 deletions

View File

@ -109,21 +109,20 @@ func (serversInfo *ServersInfo) refreshServer(proxy *Proxy, name string, stamp s
} }
newServer.rtt = ewma.NewMovingAverage(RTTEwmaDecay) newServer.rtt = ewma.NewMovingAverage(RTTEwmaDecay)
newServer.rtt.Set(float64(newServer.initialRtt)) newServer.rtt.Set(float64(newServer.initialRtt))
isNew := true
serversInfo.Lock() serversInfo.Lock()
defer serversInfo.Unlock()
previousIndex = -1
for i, oldServer := range serversInfo.inner { for i, oldServer := range serversInfo.inner {
if oldServer.Name == name { if oldServer.Name == name {
previousIndex = i serversInfo.inner[i] = &newServer
isNew = false
break break
} }
} }
if previousIndex >= 0 { if isNew {
serversInfo.inner[previousIndex] = &newServer serversInfo.inner = append(serversInfo.inner, &newServer)
return nil serversInfo.registeredServers = append(serversInfo.registeredServers, RegisteredServer{name: name, stamp: stamp})
} }
serversInfo.inner = append(serversInfo.inner, &newServer) serversInfo.Unlock()
serversInfo.registeredServers = append(serversInfo.registeredServers, RegisteredServer{name: name, stamp: stamp})
return nil return nil
} }