Files
httpx/balancer/roundrobin.go
Aleksey Shakhmatov 8d322123a4 Add load balancer with round-robin, failover, and weighted strategies
Implements balancer middleware with URL rewriting per-request:
- RoundRobin, Failover, and WeightedRandom endpoint selection strategies
- Background HealthChecker with configurable probe interval and path
- Thread-safe health state tracking with sync.RWMutex
2026-03-20 14:22:07 +03:00

22 lines
462 B
Go

package balancer
import "sync/atomic"
type roundRobin struct {
counter atomic.Uint64
}
// RoundRobin returns a strategy that cycles through healthy endpoints
// sequentially using an atomic counter.
func RoundRobin() Strategy {
return &roundRobin{}
}
func (r *roundRobin) Next(healthy []Endpoint) (Endpoint, error) {
if len(healthy) == 0 {
return Endpoint{}, ErrNoHealthy
}
idx := r.counter.Add(1) - 1
return healthy[idx%uint64(len(healthy))], nil
}