Overview
TheBackendPool class maintains a collection of backend servers and their health status. It provides methods to query backends, filter by health status, and update health states based on health checks or proxy errors.
Constructor
Creates a new BackendPool with the specified backend server URLs.Array of backend server URLs to include in the pool. Each URL should be a complete HTTP/HTTPS endpoint (e.g.,
http://localhost:3001).- Each URL is converted to a
Backendobject with initialhealth: truestatus - All backends start in a healthy state by default
Backend interface
TheBackend interface represents a single backend server in the pool.
The complete URL of the backend server (e.g.,
http://localhost:3001)Health status of the backend.
true indicates healthy and available, false indicates unhealthy and should not receive traffic.Methods
getAllBackends()
Returns all backends in the pool regardless of health status.Backend[] - Array of all backend objects in the pool.
Use case: Primarily used by the health checker to verify all backends, including those currently marked unhealthy.
getHealthyBackends()
Returns only the backends that are currently marked as healthy.Backend[] - Array of backend objects where health === true.
Use case: Used by the load balancer to select from available backends when routing requests.
markHealthy()
Marks a specific backend as healthy and available for routing traffic.The URL of the backend to mark as healthy.
- Searches for a backend with matching URL
- Sets
health: trueif found - No-op if URL doesn’t match any backend in the pool
markUnhealthy()
Marks a specific backend as unhealthy and removes it from rotation.The URL of the backend to mark as unhealthy.
- Searches for a backend with matching URL
- Sets
health: falseif found - No-op if URL doesn’t match any backend in the pool
- Triggered by failed health checks or proxy errors
Usage examples
Creating a backend pool
Querying backend health
Updating health status
Integration with health checker
Handling proxy failures
Related
- LoadBalancer - Uses BackendPool to select backends
- HealthChecker - Updates backend health status
- ProxyHandler - Marks backends unhealthy on proxy failures