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
22 lines
462 B
Go
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
|
|
}
|