balancer

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const HealthCheckInterval time.Duration = 30 * time.Second // TODO: make it customizable

HealthCheckInterval - Health Check Frequency.

View Source
const LeastConnectionsResetInterval time.Duration = 5 * time.Minute // TODO: make it customizable

LeastConnectionsResetInterval - How often reset internal counter for Least Connection LoadBalancer.

Variables

View Source
var ErrNoAvailableItem = errors.New("no item is available")

ErrNoAvailableItem no item is available.

Functions

func CheckHealth added in v1.3.1

func CheckHealth(b *NodeBalancer, host string, config config.HealthCheck)

CheckHealth - Periodic check on nodes status.

func DoHealthCheck added in v1.3.1

func DoHealthCheck(v *Item, host string, config config.HealthCheck)

func GetUpstreamNode added in v1.3.1

func GetUpstreamNode(name string, requestURL url.URL, defaultHost string) string

GetUpstreamNode - Returns backend server using current algorithm.

func Init added in v1.3.1

func Init(name string, config config.Upstream)

Init - Initialise the LB algorithm.

func InitIpHash added in v1.3.1

func InitIpHash(name string, config config.Upstream, enableHealthchecks bool)

InitIpHash - Initialise the LB algorithm for ip-hash selection.

func InitLeastConnection added in v1.3.1

func InitLeastConnection(name string, config config.Upstream, enableHealthchecks bool)

InitLeastConnection - Initialise the LB algorithm for least-connection selection.

func InitRandom added in v1.3.1

func InitRandom(name string, config config.Upstream, enableHealthchecks bool)

InitRandom - Initialise the LB algorithm for random selection.

func InitRoundRobin

func InitRoundRobin(name string, config config.Upstream, enableHealthchecks bool)

InitRoundRobin - Initialise the LB algorithm for round robin selection.

Types

type Balancer added in v1.3.1

type Balancer interface {
	GetHealthyNodes() []Item
	Pick(requestURL string) (string, error)
	GetNodeBalancer() *NodeBalancer
}

Balancer - Represents a Load Balancer interface.

type IpHashBalancer added in v1.3.1

type IpHashBalancer struct {
	NodeBalancer
	// contains filtered or unexported fields
}

IpHashBalancer instance.

func NewIpHashBalancer added in v1.3.1

func NewIpHashBalancer(name string, items []Item) *IpHashBalancer

NewIpHashBalancer - Creates a new instance.

func (*IpHashBalancer) Pick added in v1.3.1

func (b *IpHashBalancer) Pick(requestURL string) (string, error)

Pick - Chooses next available item.

type Item added in v1.3.1

type Item struct {
	Healthy  bool
	Endpoint string
}

Item - Represents a load balanced node.

type LeastConnectionsBalancer added in v1.3.1

type LeastConnectionsBalancer struct {
	NodeBalancer
	// contains filtered or unexported fields
}

LeastConnectionsBalancer instance.

func NewLeastConnectionsBalancer added in v1.3.1

func NewLeastConnectionsBalancer(name string, items []Item) *LeastConnectionsBalancer

NewLeastConnectionsBalancer - Creates a new instance.

func (*LeastConnectionsBalancer) Pick added in v1.3.1

func (b *LeastConnectionsBalancer) Pick(requestURL string) (string, error)

Pick - Chooses next available item.

func (*LeastConnectionsBalancer) ResetCounter added in v1.3.1

func (b *LeastConnectionsBalancer) ResetCounter(period time.Duration)

ResetCounter - Resets internal connection counters periodically.

type LoadBalancing added in v1.3.1

type LoadBalancing map[string]Balancer

LoadBalancing - Contains the multiple instances for the active servers.

type NodeBalancer added in v1.3.1

type NodeBalancer struct {
	M sync.RWMutex

	Id    string
	Items []Item
}

NodeBalancer - Core structure for a load balancer.

func (*NodeBalancer) GetHealthyNodes added in v1.3.1

func (b *NodeBalancer) GetHealthyNodes() []Item

GetHealthyNodes - Retrieves healthy nodes. It locks internally: callers must NOT hold b.M when invoking it (nested RLock acquisition can deadlock with a pending writer).

func (*NodeBalancer) GetNodeBalancer added in v1.3.1

func (b *NodeBalancer) GetNodeBalancer() *NodeBalancer

GetNodeBalancer - Returns the embedded NodeBalancer, promoted to every concrete balancer type so Init* can health-check without a type assertion.

type RandomBalancer added in v1.3.1

type RandomBalancer struct {
	NodeBalancer
	// contains filtered or unexported fields
}

RandomBalancer instance.

func NewRandomBalancer added in v1.3.1

func NewRandomBalancer(name string, items []Item) *RandomBalancer

NewRandomBalancer - Creates a new instance.

func (*RandomBalancer) Pick added in v1.3.1

func (b *RandomBalancer) Pick(requestURL string) (string, error)

Pick - Chooses next available item.

type RoundRobinBalancer added in v1.3.1

type RoundRobinBalancer struct {
	NodeBalancer
	// contains filtered or unexported fields
}

RoundRobinBalancer instance.

func NewRoundRobinBalancer added in v1.3.1

func NewRoundRobinBalancer(name string, items []Item) *RoundRobinBalancer

NewRoundRobinBalancer - Creates a new instance.

func (*RoundRobinBalancer) Pick added in v1.3.1

func (b *RoundRobinBalancer) Pick(requestURL string) (string, error)

Pick - Chooses next available item.

Jump to

Keyboard shortcuts

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