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
This commit is contained in:
21
balancer/roundrobin.go
Normal file
21
balancer/roundrobin.go
Normal file
@@ -0,0 +1,21 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user