Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in LeastConnSelection Policy used in reverseproxy #5609

Closed
yashkundu opened this issue Jul 1, 2023 · 6 comments · Fixed by #5862
Closed

Bug in LeastConnSelection Policy used in reverseproxy #5609

yashkundu opened this issue Jul 1, 2023 · 6 comments · Fixed by #5862
Labels
bug 🐞 Something isn't working

Comments

@yashkundu
Copy link

There is a BUG in the way LeastConnSelection policy is selecting an upstream for the reverse proxy.
The following is the code of the select method of LeastConnSelection struct which chooses the upstream:
caddy/blob/master/modules/caddyhttp/reverseproxy/selectionpolicies.go

// Select selects the up host with the least number of connections in the
// pool. If more than one host has the same least number of connections,
// one of the hosts is chosen at random.
func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request, _ http.ResponseWriter) *Upstream {
	var bestHost *Upstream
	var count int
	leastReqs := -1

	for _, host := range pool {
		if !host.Available() {
			continue
		}
		numReqs := host.NumRequests()
		if leastReqs == -1 || numReqs < leastReqs {
			leastReqs = numReqs
			count = 0
		}

		// among hosts with same least connections, perform a reservoir
		// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
		if numReqs == leastReqs {
			count++
			if count > 1 || (weakrand.Int()%count) == 0 { //nolint:gosec
				bestHost = host
			}
		}
	}

	return bestHost
}

This always selects the last upstream which has the least number of active requests, but it should randomly choose among all the upstreams which have the least number of active requests, reservoir sampling is implemented incorrecly, because of the extra condition (count>1) in the if clause.

@francislavoie
Copy link
Member

Oh I think that was meant to be an && and not an ||. The point of the change in b19946f to add that clause is that x % 1 == 0 is always true so there's no use in generating a random number in that case. Minor optimization.

@yashkundu
Copy link
Author

I think we can use the condition:

if (count == 1) || (weakrand.Int()%count)

It will handle the condition: if the count is 1, random number won't be generated, otherwise will be generated. @francislavoie

@mholt
Copy link
Member

mholt commented Jul 10, 2023

Does this still need to be fixed, @yashkundu ?

@yashkundu
Copy link
Author

@mholt , yes, It's a small fix, I can fix it and raise a pr?

@mholt
Copy link
Member

mholt commented Jul 17, 2023

That would be splendid 🙏 thank you!

@francislavoie
Copy link
Member

FYI @yashkundu #5862

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants