feat: Add least (weighted) connections load balancing#821
Open
simbiont666 wants to merge 3 commits intocloudflare:mainfrom
Open
feat: Add least (weighted) connections load balancing#821simbiont666 wants to merge 3 commits intocloudflare:mainfrom
simbiont666 wants to merge 3 commits intocloudflare:mainfrom
Conversation
simbiont666
commented
Feb 19, 2026
Comment on lines
-86
to
-91
| // We pass the state store least connections through config so that | ||
| // when the selector is rebuilt, counters for unchanged backends can be reused | ||
| let mut upstreams = LoadBalancer::<LeastConnections>::from_backends_with_config( | ||
| Backends::new(discovery::Static::try_from_iter(["1.1.1.1:443", "1.0.0.1:443"]).unwrap()), | ||
| Some(LeastConnectionsConfig::new()), | ||
| ); |
Author
There was a problem hiding this comment.
i'm not sure whether I should keep the part about creating a LoadBalancer with config
for now, I removed it so the example focuses on working with lease and CTX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #144
Hi! This is my attempt at implementing a least (weighted) connections selection algorithm.
What's here
New API on LoadBalancer
I added
select_with_lease()and select_with_lease_with()methods specifically for theLeastConnectionsselector. They work just likeselect()/select_with(), but return a(Backend, LeastConnLease)tuple. The lease should be held for the entire duration of the proxied request - in practice this means storing it inCTXstruct and letting it drop naturally when the request completes.State preservation across rebuilds
This was the trickiest part and the exact issue raised in #269 (2)- when
LoadBalancer::update()rebuilds the selector, inflight counters must survive. The solution isLeastConnStateStore, which lives insideLeastConnectionsConfigand maps backend hashes toWeak<AtomicU32>. On rebuild,Weak::upgrade()recovers any counter that still has live references (active leases or the previous selector), so inflight counts carry over seamlessly. If you don't need state persistence across updates (static backends), the defaultbuild()path works out of the box with no extra config.Notes
Let me know if anything looks off or could be done better. Thanks!