diff --git a/cmix/gateway/hostPool.go b/cmix/gateway/hostPool.go index 65d5fb7c4d9302b8c7b57ef8a3fdcc4c0096d41e..3c5582fbadb560c485b76c77be9cda353b40d835 100644 --- a/cmix/gateway/hostPool.go +++ b/cmix/gateway/hostPool.go @@ -318,10 +318,11 @@ func (h *HostPool) initialize(startIdx uint32) error { // Begin trying gateways c := make(chan gatewayDuration, numGatewaysToTry) - exit := false - i := uint32(0) - for !exit { - for ; i < numGateways; i++ { + + i := 0 + for exit := false; !exit; { + triedHosts := uint32(0) + for ; triedHosts < numGateways && i < len(randomGateways); i++ { // Select a gateway not yet selected gwId, err := randomGateways[i].GetGatewayId() if err != nil { @@ -332,6 +333,7 @@ func (h *HostPool) initialize(startIdx uint32) error { if _, ok := h.hostMap[*gwId]; ok { continue } + triedHosts++ go func() { // Obtain that GwId's Host object @@ -352,19 +354,22 @@ func (h *HostPool) initialize(startIdx uint32) error { // Collect ping results pingTimeout := 2 * h.poolParams.HostParams.PingTimeout timer := time.NewTimer(pingTimeout) + + newAppends := uint32(0) innerLoop: for { select { case gw := <-c: // Only add successful pings if gw.latency > 0 { + newAppends++ resultList = append(resultList, gw) jww.DEBUG.Printf("Adding HostPool result %d/%d: %s: %d", len(resultList), numGatewaysToTry, gw.id, gw.latency) } // Break if we have all needed slots - if uint32(len(resultList)) == numGatewaysToTry || i >= numGateways { + if newAppends == triedHosts { exit = true timer.Stop() break innerLoop @@ -376,6 +381,10 @@ func (h *HostPool) initialize(startIdx uint32) error { break innerLoop } } + + if i >= len(randomGateways) { + exit = true + } } // Sort the resultList by lowest latency