Overview
TheLoadBalancer class is the central component of the HTTP load balancer. It coordinates between the backend pool and the load balancing strategy to select the optimal backend server for each incoming request.
Constructor
Creates a new LoadBalancer instance with the specified backend pool and load balancing strategy.The pool of backend servers to distribute requests across. Maintains the health status of all backends.
The load balancing strategy to use for selecting backends. Currently supports round-robin distribution.
Methods
pickBackend()
Selects the next healthy backend server to handle an incoming request based on the configured strategy.Backend - The selected backend server object containing url and health properties.
Throws: Error if no healthy backends are available.
Behavior:
- Retrieves all healthy backends from the backend pool
- Delegates selection to the configured strategy (e.g., round-robin)
- Returns a Backend object with the selected server’s URL and health status
Usage examples
Basic setup
Selecting a backend
Integration with proxy handler
Related
- BackendPool - Manages the collection of backend servers
- HealthChecker - Monitors backend server health
- ProxyHandler - Proxies requests to selected backends