Documentation
¶
Index ¶
- Variables
- type LoadBalancer
- func Random[E any](items ...E) (LoadBalancer[E], error)
- func RandomFromRing[E any](ring *container.Ring[E]) (LoadBalancer[E], error)
- func RoundRobin[E any](items ...E) (LoadBalancer[E], error)
- func RoundRobinFromRing[E any](ring *container.Ring[E]) (LoadBalancer[E], error)
- func WeightedRandom[E any](items ...Weighted[E]) (LoadBalancer[E], error)
- func WeightedRoundRobin[E any](items ...Weighted[E]) (LoadBalancer[E], error)
- type Weighted
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyLoadBalancer = errors.New("empty load balancer")
ErrEmptyLoadBalancer is returned when a load balancer is initialized with no valid items.
Functions ¶
This section is empty.
Types ¶
type LoadBalancer ¶
type LoadBalancer[E any] interface { // Len returns the number of elements in the load balancer. Len() int // Next returns the next element according to the load balancing strategy. Next() E // Link merges the given ring into the load balancer, inserting its elements. Link(*container.Ring[E]) LoadBalancer[E] // Unlink removes n elements from the load balancer, starting from the next position. Unlink(int) LoadBalancer[E] }
LoadBalancer defines an interface for load balancing algorithms. It provides methods to access and manipulate a circular list of elements of type E, ensuring thread-safe operations for concurrent use.
func Random ¶
func Random[E any](items ...E) (LoadBalancer[E], error)
Random creates a new random load balancer with the given items. Each item appears once in the pool. It returns error with ErrEmptyLoadBalancer if no items are provided.
func RandomFromRing ¶ added in v0.1.81
func RandomFromRing[E any](ring *container.Ring[E]) (LoadBalancer[E], error)
RandomFromRing creates a new random load balancer from an existing ring. It uses the provided ring directly, ensuring thread-safe random selection. It returns error with ErrEmptyLoadBalancer if the ring is nil or empty.
func RoundRobin ¶
func RoundRobin[E any](items ...E) (LoadBalancer[E], error)
RoundRobin creates a new round-robin load balancer with the given items. Each item appears once in the rotation. It returns error with ErrEmptyLoadBalancer if no items are provided.
func RoundRobinFromRing ¶ added in v0.1.81
func RoundRobinFromRing[E any](ring *container.Ring[E]) (LoadBalancer[E], error)
RoundRobinFromRing creates a new round-robin load balancer from an existing ring. It uses the provided ring directly, ensuring thread-safe operations. It returns error with ErrEmptyLoadBalancer if the ring is nil or empty.
func WeightedRandom ¶
func WeightedRandom[E any](items ...Weighted[E]) (LoadBalancer[E], error)
WeightedRandom creates a new weighted random load balancer. Each item's weight determines how many times it appears in the pool. It returns error with ErrEmptyLoadBalancer if no items have positive weight.
func WeightedRoundRobin ¶
func WeightedRoundRobin[E any](items ...Weighted[E]) (LoadBalancer[E], error)
WeightedRoundRobin creates a new weighted round-robin load balancer. Each item's weight determines how many times it appears in the rotation. It returns error with ErrEmptyLoadBalancer if no items have positive weight.
type Weighted ¶
type Weighted[E any] struct { Item E // The item to be balanced. Weight int // The weight determining the item's frequency in the rotation. }
Weighted represents an item with an associated weight for weighted load balancing. The Weight field determines how many times the Item appears in the load balancer.