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:
2026-03-20 14:22:07 +03:00
parent 2ca930236d
commit 8d322123a4
7 changed files with 557 additions and 0 deletions

17
balancer/failover.go Normal file
View File

@@ -0,0 +1,17 @@
package balancer
type failover struct{}
// Failover returns a strategy that always picks the first healthy endpoint.
// If the primary endpoint is unhealthy, it falls back to the next available
// healthy endpoint in order.
func Failover() Strategy {
return &failover{}
}
func (f *failover) Next(healthy []Endpoint) (Endpoint, error) {
if len(healthy) == 0 {
return Endpoint{}, ErrNoHealthy
}
return healthy[0], nil
}