caddy/modules/caddyhttp
TowyTowy b2be548275
reverseproxy: fix broken reservoir sampling in random_choose policy (#7873)
The random_choose selection policy is meant to implement
power-of-d-choices: sample d available upstreams uniformly, then pick
the least-loaded of that sample. The sampling loop, however, was not a
correct reservoir sample (Algorithm R): it never filled the first k
reservoir slots unconditionally, instead writing every candidate to a
random slot j = rand(i+1), which can evict an earlier candidate while
leaving another slot nil. It also derived j from the upstream's index
in the pool rather than from the number of available upstreams seen,
skewing the sample whenever unavailable upstreams precede available
ones.

As a result the reservoir frequently held fewer than min(k, available)
upstreams, so the least-load comparison often never happened. Most
notably, with two upstreams and 'random_choose 2' (the canonical
power-of-two-choices setup), half of all requests were routed to the
more-loaded upstream even when it was saturated and the other idle --
identical behavior to plain 'random'. The nil slots this leaves behind
were also the cause of the panic reported in #3810, which was patched
by skipping nils in leastRequests rather than by fixing the sampling.

Replace the loop with a standard Algorithm R reservoir sample over the
available upstreams: fill the first k slots, then replace a random slot
with probability k/n. Every available upstream is now sampled uniformly
and the reservoir always holds min(k, available) candidates.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 09:25:31 +10:00
..
caddyauth caddyauth: add candidate placeholders for rejected identities (#7698) 2026-05-20 13:51:54 +00:00
encode encode: add standard benchmark and conformance harness (#7804) 2026-06-11 17:55:18 -06:00
fileserver browse: Update Caddy logo 2026-06-20 13:45:10 -06:00
headers
intercept intercept: fix misleading handle_response error for extra matcher args (#7871) 2026-07-12 09:16:34 +10:00
logging
map
proxyprotocol
push
requestbody
reverseproxy reverseproxy: fix broken reservoir sampling in random_choose policy (#7873) 2026-07-12 09:25:31 +10:00
rewrite rewrite: scope keyed query replace to its named key (#7818) 2026-06-16 01:01:05 +10:00
standard
templates templates: Patch for GHSA-vcc4-2c75-vc9v (#7785) 2026-06-01 13:35:02 -06:00
tracing tracing: fix BatchSpanProcessor goroutine leak on config reload (#7826) 2026-06-18 11:42:58 -06:00
app.go caddyhttp: Document dropping underscore headers 2026-06-20 13:45:10 -06:00
autohttps.go
autohttps_test.go
caddyhttp.go
caddyhttp_test.go
celmatcher.go build(deps): bump cel-go from v0.28.1 to v0.29.2 (#7872) 2026-07-12 09:17:08 +10:00
celmatcher_test.go caddyhttp: add URL pattern request matcher (#7787) 2026-06-22 10:25:43 -06:00
errors.go
http2listener.go chore: clean up wording and typo fixes (#7745) 2026-05-20 16:36:30 +10:00
httpredirectlistener.go chore: clean up wording and typo fixes (#7745) 2026-05-20 16:36:30 +10:00
invoke.go
ip_matchers.go
ip_range.go
logging.go
marshalers.go
matchers.go caddyhttp: fix path_regexp (MatchPathRE) Windows backslash bypass (#7858) 2026-07-11 08:37:44 +10:00
matchers_test.go caddyhttp: fix path_regexp (MatchPathRE) Windows backslash bypass (#7858) 2026-07-11 08:37:44 +10:00
metrics.go
metrics_test.go
replacer.go caddyhttp: add {http.request.proto_name} placeholder for spec-compliant protocol names (#7782) 2026-06-04 11:03:19 -04:00
replacer_test.go caddyhttp: add {http.request.proto_name} placeholder for spec-compliant protocol names (#7782) 2026-06-04 11:03:19 -04:00
responsematchers.go
responsematchers_test.go
responsewriter.go
responsewriter_test.go
routes.go
server.go caddyhttp: New expected_underscore_headers server option (#7809) 2026-06-11 22:26:21 -06:00
server_test.go caddyhttp: New expected_underscore_headers server option (#7809) 2026-06-11 22:26:21 -06:00
staticerror.go
staticresp.go
staticresp_test.go
subroute.go
urlpatternmatcher.go caddyhttp: add URL pattern request matcher (#7787) 2026-06-22 10:25:43 -06:00
urlpatternmatcher_test.go caddyhttp: add URL pattern request matcher (#7787) 2026-06-22 10:25:43 -06:00
vars.go caddyhttp: Clean up variable scope in vars matcher 2026-06-20 13:45:10 -06:00
vars_test.go