Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 03:00 UTC
reverseproxy: Optimize base case for least_conn and random_choose policies (#5487)
When only a single request has the least amount of requests, there's no need to compute a random number, because the modulo of 1 will always be 0 anyways.
Francis Lavoie committed
May 5, 2023 at 22:53 UTC
b19946f6af75a00bb1f3d094a903224541a70fcd
1 file changed
+4
-1
modules/caddyhttp/reverseproxy/selectionpolicies.go
+4
-1
@@ -187,7 +187,7 @@ func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request, _ http.Resp
187
// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
188
if numReqs == leastReqs {
189
count++
190
- if (weakrand.Int() % count) == 0 { //nolint:gosec
190
+ if count > 1 || (weakrand.Int()%count) == 0 { //nolint:gosec
191
bestHost = host
192
}
193
}
@@ -707,6 +707,9 @@ func leastRequests(upstreams []*Upstream) *Upstream {
707
if len(best) == 0 {
708
return nil
709
}
710
+ if len(best) == 1 {
711
+ return best[0]
712
+ }
713
return best[weakrand.Intn(len(best))] //nolint:gosec
714
}
715