lb

package
v0.0.0-...-91afcf1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ConfigKey carries *Config into lb.Run via context.
	//   ctx = context.WithValue(ctx, lb.ConfigKey, &lb.Config{...})
	ConfigKey contextKey = "lb-config"
)

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, reg *Registry) error

func RunHealthChecker

func RunHealthChecker(ctx context.Context, cfg *Config, reg *Registry)

Types

type Algorithm

type Algorithm int
const (
	RoundRobin Algorithm = iota // default
	LeastConns                  // fewest active connections
	RandomPick                  // uniform random
)

func (Algorithm) String

func (a Algorithm) String() string

type Backend

type Backend struct {
	// Addr is the TCP address of this backend. e.g. "localhost:8080".
	// Immutable after creation.
	Addr string
	// contains filtered or unexported fields
}

Backend represents a single upstream server. All fields that are read concurrently use atomics — no mutex on the hot path.

func (*Backend) ActiveConns

func (b *Backend) ActiveConns() int64

ActiveConns returns the current live connection count for this backend.

func (*Backend) Healthy

func (b *Backend) Healthy() bool

Healthy returns true if the backend passed its last health check.

func (*Backend) Snapshot

func (b *Backend) Snapshot() BackendSnapshot

Snapshot returns a point-in-time copy of this backend's counters for display.

type BackendSnapshot

type BackendSnapshot struct {
	Addr        string
	Healthy     bool
	ActiveConns int64
	TotalConns  int64
	TotalBytes  int64
}

BackendSnapshot is a plain struct copy of Backend counters — safe to print without holding any locks.

type Config

type Config struct {
	// Port the LB listens on for incoming client connections.
	Port string

	// ID shown in logs.
	ID string

	// HealthInterval is how often the health checker probes each backend.
	// Defaults to 3 seconds if zero.
	HealthInterval int // seconds
	// contains filtered or unexported fields
}

Config holds everything lb.Run needs. Port and ID are immutable. Algorithm can be changed at runtime by the CLI.

func (*Config) Algo

func (c *Config) Algo() Algorithm

Algo returns the current picking algorithm.

func (*Config) SetAlgo

func (c *Config) SetAlgo(a Algorithm)

SetAlgo atomically updates the picking algorithm.

type Registry

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

func (*Registry) Add

func (r *Registry) Add(addr string) *Backend

Add registers a new backend. No-op if addr is already registered.

func (*Registry) All

func (r *Registry) All() []*Backend

All returns a snapshot of every backend regardless of health — used by CLI display.

func (*Registry) Healthy

func (r *Registry) Healthy() []*Backend

Healthy returns a snapshot of all currently healthy backends. Called by the picker on every new connection.

func (*Registry) Remove

func (r *Registry) Remove(addr string) bool

Remove deregisters a backend by address.

Jump to

Keyboard shortcuts

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