lb

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashBytes

func HashBytes(b []byte) uint64

HashBytes processes the payload rapidly targeting the underlying architecture. Uses hardware-accelerated CRC32 instructions for routing resolutions.

func HashString

func HashString(s string) uint64

HashString processes the payload rapidly targeting the underlying architecture. Uses hardware-accelerated CRC32 instructions for routing resolutions.

func HashUint64

func HashUint64(x uint64) uint64

HashUint64 retains xxhash purely to satisfy distribution qualities required by consistent hashing. Evaluates integer placements uniformly avoiding clustered collisions heavily.

Types

type Activity

type Activity interface {
	InFlight() int64
	ResponseTime() int64
}

type Adaptive

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

Adaptive load balancer wrapper. Leverages Backend's native atomic metrics (ResponseTime, InFlight) for lock-free, zero-allocation routing decisions using epsilon-greedy strategy.

func NewAdaptive

func NewAdaptive(child Balancer, learningRate float64) *Adaptive

NewAdaptive creates a lock-free adaptive load balancer. learningRate controls exploration probability (0-1). Clamped to 0.15 if invalid.

func (*Adaptive) Backends

func (s *Adaptive) Backends() []Backend

func (*Adaptive) Pick

func (s *Adaptive) Pick(r *http.Request, keyFunc func() uint64) Backend

func (*Adaptive) Stop

func (s *Adaptive) Stop()

func (*Adaptive) Unwrap added in v0.0.6

func (s *Adaptive) Unwrap() Balancer

Unwrap returns the underlying balancer for chain inspection.

func (*Adaptive) Update

func (s *Adaptive) Update(backends []Backend)

type Backend

type Backend interface {
	Activity
	Status(v bool)
	Alive() bool
	IsUsable() bool
	Weight() int
}

type Balancer

type Balancer interface {
	Pick(r *http.Request, keyFunc func() uint64) Backend
	Update(backends []Backend)
	Backends() []Backend
	Stop()
}

type Consistent

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

func NewConsistent

func NewConsistent(count int, replicas int) *Consistent

NewConsistent generates a distributed hashing ring mapping backends securely. Multiplies the total surface area by the specified replica count internally.

func (*Consistent) Get

func (r *Consistent) Get(key uint64) int

Get locates the designated backend mapping for a specific cryptographic hash. Delegates to hardware-accelerated binary search logic for maximum speed.

type Selector

type Selector struct {
	Strategy Strategy
	// contains filtered or unexported fields
}

func NewSelector

func NewSelector(backends []Backend, strategy Strategy) *Selector

func (*Selector) Backends

func (s *Selector) Backends() []Backend

func (*Selector) Pick

func (s *Selector) Pick(r *http.Request, keyFunc func() uint64) Backend

func (*Selector) Stop

func (s *Selector) Stop()

func (*Selector) Update

func (s *Selector) Update(backends []Backend)

type Sticky

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

Sticky adds session persistence using jack.Reaper for efficient TTL management

func NewSticky

func NewSticky(child Balancer, ttl time.Duration, extractor func(*http.Request) string) *Sticky

NewSticky wraps a balancer with session affinity. Uses jack.Reaper for O(log N) expiration and mappo.LRU for bounded caching.

func (*Sticky) Backends

func (s *Sticky) Backends() []Backend

func (*Sticky) GetStats

func (s *Sticky) GetStats() map[string]any

GetStats returns statistics about the sticky table.

func (*Sticky) Pick

func (s *Sticky) Pick(r *http.Request, keyFunc func() uint64) Backend

Pick selects a backend with session affinity.

func (*Sticky) Stop

func (s *Sticky) Stop()

Stop gracefully shuts down the reaper and child balancer.

func (*Sticky) Unwrap added in v0.0.6

func (s *Sticky) Unwrap() Balancer

Unwrap returns the underlying balancer for chain inspection.

func (*Sticky) Update

func (s *Sticky) Update(backends []Backend)

Update propagates update and clears invalid sticky sessions.

type Strategy

type Strategy uint8
const (
	StrategyRoundRobin Strategy = iota
	StrategyRandom
	StrategyLeastConn
	StrategyWeightedLeastConn
	StrategyIPHash
	StrategyURLHash
	StrategyLeastResponseTime
	StrategyPowerOfTwoChoices
	StrategyConsistentHash
)

func ParseStrategy

func ParseStrategy(s string) Strategy

ParseStrategy translates string definitions into load balancing enumerators securely. Assigns the standard RoundRobin approach if matching configurations fail.

type WeightWheel

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

func NewWheel

func NewWheel(weights []int) *WeightWheel

NewWheel constructs a distributed scale mapped directly to backend capacities. Normalizes empty variables guarding against zero-sum divisions implicitly.

func (*WeightWheel) Next

func (w *WeightWheel) Next(counter uint64) int

Next generates an isolated offset translating counters into array indices. Redirects processing immediately if wheel configurations stand empty.

func (*WeightWheel) RandomIndex

func (w *WeightWheel) RandomIndex(r *rand.Rand) int

RandomIndex processes asynchronous numeric ranges avoiding predictable cycles. Empowers random routing selectors efficiently mapping capacities uniformly.

Jump to

Keyboard shortcuts

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