Documentation ¶
Overview ¶
Package balancer provides simple well-known distribution algorithms for easy traffic balancing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultBalancer = NewRoundRobin()
DefaultBalancer is the default balancer used by the middleware.
var DefaultErrorHandler = func(err error, w http.ResponseWriter, r *http.Request, h http.Handler) {
h.ServeHTTP(w, r)
}
DefaultErrorHandler is used as default error handler, which is a no-op handler.
var ErrNoServers = errors.New("balancer: server list is empty")
ErrNoServers is returned when no servers are passed to the balancer.
Functions ¶
This section is empty.
Types ¶
type Balancer ¶
type Balancer interface { // Balance balances the given servers and returns // the next server or an error. Balance([]string) (string, error) }
Balancer represents the interface implemented by balancer providers.
type ErrorHandler ¶
ErrorHandler represents the required interface implemented by balancer error handlers.
type ProxyBalancer ¶
type ProxyBalancer struct { // Servers stores the list of server URLs to balance. Servers []string // Balancer stores the balancer to be used. Balancer Balancer // ErrorHandler optionally stores the error handler to handle the balancer error and reply accordingly. ErrorHandler ErrorHandler }
ProxyBalancer balances an incoming HTTP request across a pool of server based on the selected balancing algorithm.
func New ¶
func New(servers ...string) *ProxyBalancer
New creates a new balancer which will balance traffic across the specific URLs.
func (*ProxyBalancer) BalanceHTTP ¶
func (b *ProxyBalancer) BalanceHTTP(w http.ResponseWriter, r *http.Request, h http.Handler)
BalanceHTTP handles an incoming HTTP request and defines the target server to forward the request.
func (*ProxyBalancer) OnError ¶
func (b *ProxyBalancer) OnError(handler ErrorHandler)
OnError defines the error handler to be used to handle the balancer error.
func (*ProxyBalancer) Register ¶
func (b *ProxyBalancer) Register(mw layer.Middleware)
Register registers the middleware.
type Random ¶
type Random struct {
// contains filtered or unexported fields
}
Random is a simple load balance using a random seed for balancing.
type RoundRobin ¶
type RoundRobin struct {
// contains filtered or unexported fields
}
RoundRobin is a simple load balancer that returns each of the published endpoints in sequence.
func NewRoundRobin ¶
func NewRoundRobin() *RoundRobin
NewRoundRobin returns a new RoundRobin load balancer.