Now the connection tracking and the least-connections algorithm meet. By holding several leases open at once, you can watch least-connections spread work across the idle backends - the payoff for wiring active counts into Begin.
Show that with leases held open, least-connections routes each new request to a backend that is not yet busy.
This is the moment the load-aware design proves itself. Begin raises a backend’s
active count, and least-connections reads that same count, so when you hold leases
open - simulating requests still in flight - each new Begin is pushed toward a
backend that is not yet busy. Three idle backends fill up one each, and only when
they are all equally loaded does the deterministic tie-break send the fourth back to
A.
Expect no new production code today: the mechanism was fully built across the active-count and lease lessons, and this test simply exercises them together. A green result with an empty diff is the right outcome and the satisfying “it works” beat for the chapter. Releasing one of the open leases would immediately make that backend the least-loaded again - a good thing to try by hand to feel the feedback loop, even though the pinned spec keeps the leases open for a clean, exact count.
// No new production code: Begin already increments Active(), and the// least-connections Select() already reads Active(). This lesson holds the// leases open (skips release) so the in-flight counts drive the next pick.b1, _, _ := bal.Begin() // A: all were 0b2, _, _ := bal.Begin() // B: A=1, rest 0b3, _, _ := bal.Begin() // C: A=1, B=1, C=0b4, _, _ := bal.Begin() // A: all 1, tie -> first