Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 03:30 UTC
reverseproxy: Pick the least loaded host in random_choose when all are busy (#8160)
Jay Hemnani committed
Oct 7, 2026 at 09:55 UTC
036895ed4bad6b1ec395ad09f7164eedbbb844e3
2 files changed
+16
-3
modules/caddyhttp/reverseproxy/selectionpolicies.go
+3
-3
@@ -840,10 +840,10 @@ func leastRequests(upstreams []*Upstream) *Upstream {
840
if reqs == 0 {
841
return upstream
842
}
843
- // If bestReqs was just initialized to -1
844
- // we need to append upstream also
845
- if reqs <= bestReqs || bestReqs == -1 {
843
+ if reqs < bestReqs || bestReqs == -1 {
844
bestReqs = reqs
845
+ best = []*Upstream{upstream}
846
+ } else if reqs == bestReqs {
847
best = append(best, upstream)
848
}
849
}
modules/caddyhttp/reverseproxy/selectionpolicies_test.go
+13
@@ -699,6 +699,19 @@ func TestLeastRequests(t *testing.T) {
699
}
700
}
701
702
+func TestLeastRequestsAllBusy(t *testing.T) {
703
+ pool := testPool()
704
+ pool[0].countRequest(30)
705
+ pool[1].countRequest(1)
706
+ pool[2].countRequest(20)
707
+
708
+ for i := 0; i < 100; i++ {
709
+ if result := leastRequests(pool); result != pool[1] {
710
+ t.Fatalf("iteration %d: got %v, want the least loaded host %v", i, result, pool[1])
711
+ }
712
+ }
713
+}
714
+
715
func TestRandomChoicePolicy(t *testing.T) {
716
pool := testPool()
717
pool[0].Dial = "localhost:8080"