tinylb

package
v0.0.0-...-8fcbe07 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package tinylb implements a small and simple userland round-robin load balancer, mostly for TCP connections. However, it is entirely protocol-agnostic, and only expects net.Listener and net.Conn objects.

Apart from the simple act of load-balancing across a set of backends, tinylb also automatically and immediately closes all open connections to backend targets that have been removed from the set. This is perhaps not the ideal behaviour for user-facing services, but it's the sort of behaviour that works very well for machine-to-machine communication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// TargetName returns the 'target name' of the backend, which is _not_ the same
	// as its key in the BackendSet. Instead, the TargetName should uniquely identify
	// some backend address, and will be used to figure out that while a backend
	// might still exist, its target address has changed - and thus, all existing
	// connections to the previous target address should be closed.
	//
	// For simple load balancing backends, this could be the connection string used
	// to connect to the backend.
	TargetName() string
	// Dial returns a new connection to this backend.
	Dial() (net.Conn, error)
}

Backend is to be implemented by different kinds of loadbalancing backends, eg. one per network protocol.

type BackendSet

type BackendSet = mapsets.OrderedMap[string, Backend]

BackendSet is the main structure used to provide the current set of backends that should be targeted by tinylb. The key is some unique backend identifier.

type Server

type Server struct {
	// Provider is some event Value which provides the current BackendSet for the
	// loadbalancer to use. As the BackendSet is updated, the internal loadbalancing
	// algorithm will adjust to the updated set, and any connections to backend
	// TargetNames that are not present in the set anymore will be closed.
	Provider event.Value[BackendSet]
	// Listener is where the loadbalancer will listen on. After the loadbalancer
	// exits, this listener will be closed.
	Listener net.Listener
}

Server is a tiny round-robin loadbalancer for net.Listener/net.Conn compatible protocols.

All fields must be set before the loadbalancer can be run.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run the loadbalancer in a superervisor.Runnable and block until canceled. Because the Server's Listener will be closed after exit, it should be opened in the same runnable as this function is then started.

type SimpleTCPBackend

type SimpleTCPBackend struct {
	Remote string
}

SimpleTCPBackend implements Backend for trivial TCP-based backends.

func (*SimpleTCPBackend) Dial

func (t *SimpleTCPBackend) Dial() (net.Conn, error)

func (*SimpleTCPBackend) TargetName

func (t *SimpleTCPBackend) TargetName() string

Jump to

Keyboard shortcuts

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