loadbalancer

package
v0.0.0-...-7143a4b Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	Name              string
	URL               *url.URL
	ReverseProxy      *httputil.ReverseProxy
	IsHealthy         bool
	UnhealthyUntil    time.Time    // Time until which the backend is considered unhealthy
	ActiveConnections int32        // Number of active connections
	Weight            int          // Weight for weighted load balancing strategies
	Mutex             sync.RWMutex // Mutex for thread-safe operations
}

Backend represents a backend server

func (*Backend) DecrementConnections

func (backend *Backend) DecrementConnections()

DecrementConnections decrements the active connection count for a backend

func (*Backend) GetActiveConnections

func (backend *Backend) GetActiveConnections() int32

GetActiveConnections returns the current number of active connections

func (*Backend) IncrementConnections

func (backend *Backend) IncrementConnections()

IncrementConnections increments the active connection count for a backend

type BackendInfo

type BackendInfo struct {
	Name              string `json:"name"`
	Address           string `json:"address"`
	Healthy           bool   `json:"healthy"`
	ActiveConnections int32  `json:"active_connections"`
	Weight            int    `json:"weight"`
}

BackendInfo is a lightweight snapshot used by the Admin API

type IPHashStrategy

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

IPHashStrategy implements an IP hash load balancing strategy.

func NewIPHashStrategy

func NewIPHashStrategy() *IPHashStrategy

NewIPHashStrategy creates a new IP hash strategy.

func (*IPHashStrategy) AddBackend

func (iph *IPHashStrategy) AddBackend(backend *Backend)

AddBackend adds a backend to the pool.

func (*IPHashStrategy) GetBackends

func (iph *IPHashStrategy) GetBackends() []*Backend

GetBackends returns all backends in the pool.

func (*IPHashStrategy) NextBackend

func (iph *IPHashStrategy) NextBackend(r *http.Request) *Backend

NextBackend returns the next backend using the IP hash algorithm.

func (*IPHashStrategy) RemoveBackend

func (iph *IPHashStrategy) RemoveBackend(backend *Backend)

RemoveBackend removes a backend from the pool.

type LeastConnectionsStrategy

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

LeastConnectionsStrategy implements a least-connections load balancing strategy

func NewLeastConnectionsStrategy

func NewLeastConnectionsStrategy() *LeastConnectionsStrategy

NewLeastConnectionsStrategy creates a new least-connections strategy

func (*LeastConnectionsStrategy) AddBackend

func (lc *LeastConnectionsStrategy) AddBackend(backend *Backend)

AddBackend adds a backend to the pool

func (*LeastConnectionsStrategy) GetBackends

func (lc *LeastConnectionsStrategy) GetBackends() []*Backend

GetBackends returns all backends in the pool

func (*LeastConnectionsStrategy) NextBackend

func (lc *LeastConnectionsStrategy) NextBackend(r *http.Request) *Backend

NextBackend returns the backend with the least active connections

func (*LeastConnectionsStrategy) RemoveBackend

func (lc *LeastConnectionsStrategy) RemoveBackend(backend *Backend)

RemoveBackend removes a backend from the pool

type LoadBalancer

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

LoadBalancer manages the backend servers and implements load balancing

func NewLoadBalancer

func NewLoadBalancer(cfg *config.Config) (*LoadBalancer, error)

NewLoadBalancer creates a new load balancer with the specified strategy

func (*LoadBalancer) AddBackend

func (lb *LoadBalancer) AddBackend(backendCfg config.BackendConfig) error

AddBackend adds a new backend server to the load balancer

func (*LoadBalancer) GetMetricsCollector

func (lb *LoadBalancer) GetMetricsCollector() *metrics.MetricsCollector

GetMetricsCollector returns the metrics collector

func (*LoadBalancer) IsBackendHealthy

func (lb *LoadBalancer) IsBackendHealthy(backend *Backend) bool

IsBackendHealthy checks if a backend is currently healthy

func (*LoadBalancer) ListBackends

func (lb *LoadBalancer) ListBackends() []BackendInfo

ListBackends returns a snapshot of backends for the Admin API

func (*LoadBalancer) MarkBackendUnhealthy

func (lb *LoadBalancer) MarkBackendUnhealthy(backend *Backend, duration time.Duration)

MarkBackendUnhealthy marks a backend as unhealthy for a specified duration

func (*LoadBalancer) NextBackend

func (lb *LoadBalancer) NextBackend(r *http.Request) *Backend

NextBackend returns the next backend server according to the strategy

func (*LoadBalancer) RemoveBackend

func (lb *LoadBalancer) RemoveBackend(name string)

RemoveBackend removes a backend server from the load balancer

func (*LoadBalancer) ServeHTTP

func (lb *LoadBalancer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface

func (*LoadBalancer) SetStrategy

func (lb *LoadBalancer) SetStrategy(name string) error

SetStrategy switches the load balancing strategy at runtime

type RoundRobinStrategy

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

RoundRobinStrategy implements a round-robin load balancing strategy

func NewRoundRobinStrategy

func NewRoundRobinStrategy() *RoundRobinStrategy

NewRoundRobinStrategy creates a new round-robin strategy

func (*RoundRobinStrategy) AddBackend

func (rr *RoundRobinStrategy) AddBackend(backend *Backend)

AddBackend adds a backend to the pool

func (*RoundRobinStrategy) GetBackends

func (rr *RoundRobinStrategy) GetBackends() []*Backend

GetBackends returns all backends in the pool

func (*RoundRobinStrategy) NextBackend

func (rr *RoundRobinStrategy) NextBackend(r *http.Request) *Backend

NextBackend returns the next backend in the rotation

func (*RoundRobinStrategy) RemoveBackend

func (rr *RoundRobinStrategy) RemoveBackend(backend *Backend)

RemoveBackend removes a backend from the pool

type Strategy

type Strategy interface {
	NextBackend(r *http.Request) *Backend
	AddBackend(backend *Backend)
	RemoveBackend(backend *Backend)
	GetBackends() []*Backend
}

Strategy defines the interface for load balancing strategies

type WeightedRoundRobinStrategy

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

WeightedRoundRobinStrategy implements a smooth weighted round-robin load balancing strategy.

func NewWeightedRoundRobinStrategy

func NewWeightedRoundRobinStrategy() *WeightedRoundRobinStrategy

NewWeightedRoundRobinStrategy creates a new weighted round-robin strategy.

func (*WeightedRoundRobinStrategy) AddBackend

func (wrr *WeightedRoundRobinStrategy) AddBackend(backend *Backend)

AddBackend adds a backend to the pool.

func (*WeightedRoundRobinStrategy) GetBackends

func (wrr *WeightedRoundRobinStrategy) GetBackends() []*Backend

GetBackends returns all backends in the pool.

func (*WeightedRoundRobinStrategy) NextBackend

func (wrr *WeightedRoundRobinStrategy) NextBackend(r *http.Request) *Backend

NextBackend returns the next backend using the smooth weighted round-robin algorithm.

func (*WeightedRoundRobinStrategy) RemoveBackend

func (wrr *WeightedRoundRobinStrategy) RemoveBackend(backend *Backend)

RemoveBackend removes a backend from the pool.

Jump to

Keyboard shortcuts

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