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
18 lines
427 B
Go
18 lines
427 B
Go
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
|
|
}
|