caddyserver/caddy · error

choose must be at least 2

Error message

choose must be at least 2

What it means

Error "choose must be at least 2" thrown in caddyserver/caddy.

Source

Thrown at modules/caddyhttp/reverseproxy/selectionpolicies.go:228

	if err != nil {
		return d.Errf("invalid choice value '%s': %v", chooseStr, err)
	}
	r.Choose = choose
	return nil
}

// Provision sets up r.
func (r *RandomChoiceSelection) Provision(ctx caddy.Context) error {
	if r.Choose == 0 {
		r.Choose = 2
	}
	return nil
}

// Validate ensures that r's configuration is valid.
func (r RandomChoiceSelection) Validate() error {
	if r.Choose < 2 {
		return fmt.Errorf("choose must be at least 2")
	}
	return nil
}

// Select returns an available host, if any.
func (r RandomChoiceSelection) Select(pool UpstreamPool, _ *http.Request, _ http.ResponseWriter) *Upstream {
	k := min(r.Choose, len(pool))

	// reservoir sampling (Algorithm R) over the available upstreams:
	// the first k available upstreams fill the reservoir, then each
	// subsequent one replaces a random reservoir entry with probability
	// k/n, so every available upstream is sampled uniformly
	choices := make([]*Upstream, 0, k)
	var available int
	for _, upstream := range pool {
		if !upstream.Available() {
			continue
		}

View on GitHub (pinned to 50e54ee279)

Solutions

  1. Set 'choose' to 2 or greater for the power of two choices selection policy.

When it happens

Trigger: Thrown at modules/caddyhttp/reverseproxy/selectionpolicies.go:228 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of caddyserver/caddy@50e54ee279 (2026-08-15). Data as JSON: /api/errors/0475c67723c85ef5. Report an issue: GitHub.