A simple load balancer implemented in Go that distributes incoming requests among three backend servers. It handles server health checks and periodically attempts to add servers back after once they go back online. The load balancer uses a least connections algorithm to distribute requests.
- Distributes requests among three backend servers.
- Performs health checks on servers to detect failures.
- Automatically recovers from server failures by adding them back to the pool.
- Uses a least connections algorithm for fair distribution of requests.
- Server: Represents a backend server with its address. It also maintains a connection count for least connections selection.
- Server Pool: Manages a collection of servers, storing them in both healthy and unhealthy states. Uses
sync.Mapfor concurrent access. - Load Balancer: Selects a healthy server from the pool and forwards the request using a reverse proxy. It also manages the health checks and recovery process.
- The
sync.Maptype is used to efficiently manage concurrent access to the server pool. - The
http.Clientwith a timeout is used for health checks. - The
httputil.NewSingleHostReverseProxyis used to forward requests to the selected server. - The
LeastConnectionsfunction implements the least connections algorithm, selecting the server with the fewest current connections.
go run main.go
go run server1\main.gogo run server2\main.gogo run server3\main.go
go run client\main.go
This is a basic implementation for educational purposes.
Maybe try to implement my own rate limiter