Skip to content

Commit 5779a79

Browse files
committed
fixed possible of infinite loop
#1625
1 parent e241a4d commit 5779a79

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

internal/balancer/local_dc.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math/rand"
88
"net"
99
"net/url"
10+
"slices"
1011
"strings"
1112
"sync"
1213

@@ -157,21 +158,12 @@ func getRandomEndpoints(endpoints []endpoint.Endpoint, count int) []endpoint.End
157158
return endpoints
158159
}
159160

160-
got := make(map[int]bool, maxEndpointsCheckPerLocation)
161+
endpoints = slices.Clone(endpoints)
162+
rand.Shuffle(len(endpoints), func(i, j int) {
163+
endpoints[i], endpoints[j] = endpoints[j], endpoints[i]
164+
})
161165

162-
res := make([]endpoint.Endpoint, 0, maxEndpointsCheckPerLocation)
163-
for len(got) < count {
164-
//nolint:gosec
165-
index := rand.Intn(len(endpoints))
166-
if got[index] {
167-
continue
168-
}
169-
170-
got[index] = true
171-
res = append(res, endpoints[index])
172-
}
173-
174-
return res
166+
return endpoints[:count]
175167
}
176168

177169
func splitEndpointsByLocation(endpoints []endpoint.Endpoint) map[string][]endpoint.Endpoint {

0 commit comments

Comments
 (0)