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

25
balancer/options.go Normal file
View File

@@ -0,0 +1,25 @@
package balancer
// options holds configuration for the load balancer transport.
type options struct {
strategy Strategy // default RoundRobin
healthChecker *HealthChecker // optional
}
// Option configures the load balancer transport.
type Option func(*options)
// WithStrategy sets the endpoint selection strategy.
// If not specified, RoundRobin is used.
func WithStrategy(s Strategy) Option {
return func(o *options) {
o.strategy = s
}
}
// WithHealthCheck enables active health checking of endpoints.
func WithHealthCheck(opts ...HealthOption) Option {
return func(o *options) {
o.healthChecker = newHealthChecker(opts...)
}
}