Round-robin ignores how busy each backend already is. Least-connections fixes that by always picking the backend handling the fewest requests right now, with a deterministic tie-break.
Select the healthy backend with the smallest active count, breaking ties by pool order.
Least-connections is load-aware: instead of blindly rotating, it sends each
request to whichever healthy backend currently has the fewest active
connections. That naturally steers traffic away from a backend that is slow or
bogged down, because its in-flight count stays high while faster backends drain and
get refilled. It reads the Active() count you added in the pool chapter.
The detail that makes it testable is the tie-break. When two backends are
equally busy you must pick one deterministically, or the sequence is unstable. Using
a strict less-than while scanning in pool order keeps the earliest backend on a
tie - so B beats C when both sit at 1. Get the comparison direction wrong (using
less-than-or-equal) and the last equal backend wins instead; pin the tie so the
choice is nailed down. The active counts are set by hand in this lesson; the
dispatch chapter is where they start moving on their own.
func (l *LeastConn) Select() (*Backend, error) {avail := l.pool.Available()best := avail[0]for _, b := range avail[1:] {if b.Active() < best.Active() { best = b } // strict < keeps the earliest on a tie}return best, nil}