aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/reverseproxy/selectionpolicies.go
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2023-05-05 22:53:48 -0400
committerGitHub <[email protected]>2023-05-05 20:53:48 -0600
commitb19946f6af75a00bb1f3d094a903224541a70fcd (patch)
treea868baf63d177883a0343c31d9ee6daaa07aa3c0 /modules/caddyhttp/reverseproxy/selectionpolicies.go
parent335cd2e8a4f2a91cb2c55a6c2e624a4c4ccddb0c (diff)
downloadcaddy-b19946f6af75a00bb1f3d094a903224541a70fcd.tar.gz
caddy-b19946f6af75a00bb1f3d094a903224541a70fcd.zip
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.
Diffstat (limited to 'modules/caddyhttp/reverseproxy/selectionpolicies.go')
-rw-r--r--modules/caddyhttp/reverseproxy/selectionpolicies.go5
1 files changed, 4 insertions, 1 deletions
diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go
index a2985f196..35fb143aa 100644
--- a/modules/caddyhttp/reverseproxy/selectionpolicies.go
+++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go
@@ -187,7 +187,7 @@ func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request, _ http.Resp
// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
if numReqs == leastReqs {
count++
- if (weakrand.Int() % count) == 0 { //nolint:gosec
+ if count > 1 || (weakrand.Int()%count) == 0 { //nolint:gosec
bestHost = host
}
}
@@ -707,6 +707,9 @@ func leastRequests(upstreams []*Upstream) *Upstream {
if len(best) == 0 {
return nil
}
+ if len(best) == 1 {
+ return best[0]
+ }
return best[weakrand.Intn(len(best))] //nolint:gosec
}