balancer

package
v0.36.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package balancer implements load balancing strategies for upstream selection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	// Next returns the next target address.
	// For connection-tracking strategies (e.g. LeastConn), this also
	// marks the target as having one more active connection.
	// Returns "" if the pool is empty.
	Next() string

	// Done signals that a request/connection to the given target has finished.
	// This is a no-op for strategies that don't track active connections.
	Done(target string)

	// SwapTargets atomically replaces the target pool.
	// Active connections tracked by Done() are reset.
	SwapTargets(targets []string)

	// Targets returns a copy of the current target pool.
	Targets() []string
}

Balancer selects a target from a pool of upstreams.

func NewByType added in v0.33.3

func NewByType(strategy string, targets []string) Balancer

NewByType creates a flat balancer of the given type. Valid types: "random", "leastconn", "roundrobin" (default).

type Grouped

type Grouped struct {
	// contains filtered or unexported fields
}

Grouped wraps a balancing strategy and provides per-key target pools. In flat mode (via SwapTargets), all requests use the inner balancer. In grouped mode (via SwapGroupedTargets), each key has its own sub-balancer.

When fallback is enabled, NextKeyed picks a random target from all groups if the requested key has no matching group (or the group is empty).

func NewGrouped

func NewGrouped(strategy string, inner Balancer) *Grouped

NewGrouped creates a grouped balancer wrapping an inner flat balancer. The strategy name is used to create per-key sub-balancers.

func (*Grouped) Done

func (g *Grouped) Done(target string)

Done decrements the active connection counter for the target. Checks grouped sub-balancers first, then falls back to the inner balancer.

func (*Grouped) Next

func (g *Grouped) Next() string

Next delegates to the inner flat balancer. When the inner pool is empty and fallback is enabled, picks a random target from all grouped targets.

func (*Grouped) NextKeyed

func (g *Grouped) NextKeyed(key string) string

NextKeyed selects a target from the sub-pool matching the key. Falls back to the inner balancer if no groups are configured or key is empty. When fallback is enabled, picks a random target from all groups if the key has no matching group or the matched group's pool is empty.

func (*Grouped) SetFallback added in v0.35.0

func (g *Grouped) SetFallback(enabled bool)

SetFallback enables or disables random fallback across all groups. When enabled, NextKeyed picks a random target from all groups if the requested key has no matching group or the group pool is empty.

func (*Grouped) SwapGroupedTargets

func (g *Grouped) SwapGroupedTargets(groups map[string][]string)

SwapGroupedTargets atomically replaces the per-key target pools. Existing sub-balancers for unchanged keys are reused (preserving connection tracking state for leastconn).

func (*Grouped) SwapTargets

func (g *Grouped) SwapTargets(targets []string)

SwapTargets replaces the flat target pool and clears any grouped state.

func (*Grouped) Targets

func (g *Grouped) Targets() []string

type KeyedBalancer

type KeyedBalancer interface {
	Balancer
	NextKeyed(key string) string
	SwapGroupedTargets(groups map[string][]string)
}

KeyedBalancer extends Balancer with key-based target selection. Used by grouped balancers where the key (e.g., a domain wildcard capture) determines which sub-pool of targets to pick from.

type LeastConn

type LeastConn struct {
	// contains filtered or unexported fields
}

LeastConn routes to the target with the fewest active connections. When multiple targets share the minimum, the first one found is selected.

func NewLeastConn

func NewLeastConn(targets []string) *LeastConn

NewLeastConn creates a least-connections balancer.

func (*LeastConn) Conns

func (lc *LeastConn) Conns(target string) int64

Conns returns the current active connection count for a target. Intended for testing and diagnostics only.

func (*LeastConn) Done

func (lc *LeastConn) Done(target string)

Done decrements the active connection counter for the target.

func (*LeastConn) Next

func (lc *LeastConn) Next() string

Next returns the target with the fewest active connections and atomically increments its counter.

func (*LeastConn) SwapTargets

func (lc *LeastConn) SwapTargets(targets []string)

SwapTargets atomically replaces the target pool. Active connection counts are reset to zero.

func (*LeastConn) Targets

func (lc *LeastConn) Targets() []string

type Random

type Random struct {
	// contains filtered or unexported fields
}

Random selects a target at random.

func NewRandom

func NewRandom(targets []string) *Random

NewRandom creates a random balancer.

func (*Random) Done

func (r *Random) Done(string)

func (*Random) Next

func (r *Random) Next() string

func (*Random) SwapTargets

func (r *Random) SwapTargets(targets []string)

func (*Random) Targets

func (r *Random) Targets() []string

type RoundRobin

type RoundRobin struct {
	// contains filtered or unexported fields
}

RoundRobin distributes requests evenly across targets in order.

func NewRoundRobin

func NewRoundRobin(targets []string) *RoundRobin

NewRoundRobin creates a round-robin balancer.

func (*RoundRobin) Done

func (rr *RoundRobin) Done(string)

func (*RoundRobin) Next

func (rr *RoundRobin) Next() string

func (*RoundRobin) SwapTargets

func (rr *RoundRobin) SwapTargets(targets []string)

func (*RoundRobin) Targets

func (rr *RoundRobin) Targets() []string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL