Fix up the final error

This commit is contained in:
YX Hao 2023-12-14 19:56:09 +08:00
parent f67cc14edd
commit 6efabc9ef3

View file

@ -224,27 +224,26 @@ func (serversInfo *ServersInfo) refresh(proxy *Proxy) (int, error) {
dlog.Debug("Refreshing certificates") dlog.Debug("Refreshing certificates")
serversInfo.RLock() serversInfo.RLock()
// Appending registeredServers slice from sources may allocate new memory. // Appending registeredServers slice from sources may allocate new memory.
registeredServers := make([]RegisteredServer, len(serversInfo.registeredServers)) serversCount := len(serversInfo.registeredServers)
registeredServers := make([]RegisteredServer, serversCount)
copy(registeredServers, serversInfo.registeredServers) copy(registeredServers, serversInfo.registeredServers)
serversInfo.RUnlock() serversInfo.RUnlock()
countChannel := make(chan struct{}, proxy.certRefreshConcurrency) countChannel := make(chan struct{}, proxy.certRefreshConcurrency)
waitChannel := make(chan struct{}) errorChannel := make(chan error, serversCount)
var err error
for i := range registeredServers { for i := range registeredServers {
countChannel <- struct{}{} countChannel <- struct{}{}
go func(registeredServer *RegisteredServer) { go func(registeredServer *RegisteredServer) {
if err = serversInfo.refreshServer(proxy, registeredServer.name, registeredServer.stamp); err == nil { err := serversInfo.refreshServer(proxy, registeredServer.name, registeredServer.stamp)
if err == nil {
proxy.xTransport.internalResolverReady = true proxy.xTransport.internalResolverReady = true
} }
errorChannel <- err
<-countChannel <-countChannel
if len(countChannel) == 0 {
close(waitChannel)
}
}(&registeredServers[i]) }(&registeredServers[i])
} }
if len(registeredServers) > 0 { var err error
<-waitChannel for i := 0; i < serversCount; i++ {
err = <-errorChannel
} }
serversInfo.Lock() serversInfo.Lock()
sort.SliceStable(serversInfo.inner, func(i, j int) bool { sort.SliceStable(serversInfo.inner, func(i, j int) bool {
@ -262,6 +261,9 @@ func (serversInfo *ServersInfo) refresh(proxy *Proxy) (int, error) {
dlog.Noticef("Server with the lowest initial latency: %s (rtt: %dms)", inner[0].Name, inner[0].initialRtt) dlog.Noticef("Server with the lowest initial latency: %s (rtt: %dms)", inner[0].Name, inner[0].initialRtt)
} }
serversInfo.Unlock() serversInfo.Unlock()
if innerLen > 0 {
err = nil
}
return innerLen, err return innerLen, err
} }