lb

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildRateLimiterMiddleware

func BuildRateLimiterMiddleware(cfg RateLimitConfig, metrics *Metrics) (func(http.Handler) http.Handler, error)

func New

func New(cfg *Config, logger *log.Logger) (*LoadBalancer, *HealthChecker, error)

Types

type Backend

type Backend struct {
	Name       string
	URL        *url.URL
	Weight     int
	HealthPath string
	// contains filtered or unexported fields
}

Backend represents one upstream service target.

func NewBackend

func NewBackend(name string, rawURL string, weight int, healthPath string) (*Backend, error)

func (*Backend) ActiveConnections

func (b *Backend) ActiveConnections() int64

func (*Backend) DecActive

func (b *Backend) DecActive()

func (*Backend) IncActive

func (b *Backend) IncActive()

func (*Backend) IsAlive

func (b *Backend) IsAlive() bool

func (*Backend) SetAlive

func (b *Backend) SetAlive(v bool)

type BackendConfig

type BackendConfig struct {
	Name       string `json:"name" yaml:"name"`
	URL        string `json:"url" yaml:"url"`
	Weight     int    `json:"weight" yaml:"weight"`
	HealthPath string `json:"health_path" yaml:"health_path"`
}

type Config

type Config struct {
	ListenAddr  string            `json:"listen_addr" yaml:"listen_addr"`
	Strategy    string            `json:"strategy" yaml:"strategy"`
	Backends    []BackendConfig   `json:"backends" yaml:"backends"`
	HealthCheck HealthCheckConfig `json:"health_check" yaml:"health_check"`
	RateLimit   RateLimitConfig   `json:"rate_limit" yaml:"rate_limit"`
}

Config describes all runtime settings for the load balancer.

func LoadConfig

func LoadConfig(path string) (*Config, error)

type ErrUnknownStrategy

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

func (ErrUnknownStrategy) Error

func (e ErrUnknownStrategy) Error() string

type HealthCheckConfig

type HealthCheckConfig struct {
	Interval      string `json:"interval" yaml:"interval"`
	Timeout       string `json:"timeout" yaml:"timeout"`
	FailThreshold int    `json:"fail_threshold" yaml:"fail_threshold"`
	PassThreshold int    `json:"pass_threshold" yaml:"pass_threshold"`
}

type HealthChecker

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

func NewHealthChecker

func NewHealthChecker(cfg HealthCheckConfig, backends []*Backend, logger *log.Logger) (*HealthChecker, error)

func (*HealthChecker) Start

func (h *HealthChecker) Start(ctx context.Context)

type LeastConnectionsStrategy

type LeastConnectionsStrategy struct{}

func (*LeastConnectionsStrategy) Name

func (s *LeastConnectionsStrategy) Name() string

func (*LeastConnectionsStrategy) Next

func (s *LeastConnectionsStrategy) Next(backends []*Backend) *Backend

type LoadBalancer

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

func (*LoadBalancer) Backends

func (lb *LoadBalancer) Backends() []*Backend

func (*LoadBalancer) Metrics

func (lb *LoadBalancer) Metrics() *Metrics

func (*LoadBalancer) ServeHTTP

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

func (*LoadBalancer) StartHealthChecks

func (lb *LoadBalancer) StartHealthChecks(ctx context.Context, checker *HealthChecker)

type Metrics

type Metrics struct {
	TotalRequests  atomic.Uint64
	TotalErrors    atomic.Uint64
	RateLimited    atomic.Uint64
	ActiveRequests atomic.Int64
	// contains filtered or unexported fields
}

Metrics captures basic operational counters.

func NewMetrics

func NewMetrics() *Metrics

func (*Metrics) Handler

func (m *Metrics) Handler(backends []*Backend) http.Handler

func (*Metrics) IncBackendError

func (m *Metrics) IncBackendError(name string)

func (*Metrics) IncBackendRequest

func (m *Metrics) IncBackendRequest(name string)

type RateLimitConfig

type RateLimitConfig struct {
	Enabled      bool    `json:"enabled" yaml:"enabled"`
	Rate         float64 `json:"rate" yaml:"rate"`
	Burst        int     `json:"burst" yaml:"burst"`
	HeatHalfLife string  `json:"heat_half_life" yaml:"heat_half_life"`
	HeatCost     float64 `json:"heat_cost" yaml:"heat_cost"`
	MaxKeys      int     `json:"max_keys" yaml:"max_keys"`
	KeyHeader    string  `json:"key_header" yaml:"key_header"`
}

type RoundRobinStrategy

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

func (*RoundRobinStrategy) Name

func (s *RoundRobinStrategy) Name() string

func (*RoundRobinStrategy) Next

func (s *RoundRobinStrategy) Next(backends []*Backend) *Backend

type Strategy

type Strategy interface {
	Name() string
	Next(backends []*Backend) *Backend
}

func NewStrategy

func NewStrategy(name string) (Strategy, error)

type WeightedRoundRobinStrategy

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

WeightedRoundRobinStrategy uses smooth weighted round robin.

func NewWeightedRoundRobinStrategy

func NewWeightedRoundRobinStrategy() *WeightedRoundRobinStrategy

func (*WeightedRoundRobinStrategy) Name

func (*WeightedRoundRobinStrategy) Next

func (s *WeightedRoundRobinStrategy) Next(backends []*Backend) *Backend

Jump to

Keyboard shortcuts

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