This Go project implements a load balancer that supports two balancing methods: Round Robin (rr) and Weighted Round Robin (wrr). It allows you to spawn local servers or read external server addresses from a configuration file (.json or .yaml) via a flag.
- Round Robin (
rr) and Weighted Round Robin (wrr) algorithms for load balancing. - Support for local server spawning or external server configuration via
.jsonor.yamlfiles. - Configurable via command-line flags.
-amount: Number of local servers to spawn (used only with-env local). Default is1234.-method: Load balancing method. Choose between:rr: Round Robin.wrr: Weighted Round Robin.
-env: Environment setting. Choose between:local: Spawns the specified amount of local servers.external: Reads server addresses from an external file (provided via-pathflag).
-path: Specifies the path to the external.jsonor.yamlconfiguration file (used with-env external).-healthCheck: Runs health check on external servers provided within a configuration file (used with-path).-port: Specifies port used to run load balancer service.-srv-port: Specifies port used to run local servers for testing purposes.
1.Spawn Local Servers (5 servers, round-robin method):
go run *.go -amount 5 -method rr -env localUse External Servers from a JSON File (weighted round-robin method):
go run *.go -method wrr -env external -path ./servers.jsonUse External Servers from a YAML File (round-robin method):
go run *.go -method rr -env external -path ./servers.yamlWhen using the -env external flag, the load balancer will read server information from a configuration file. You can provide the file in either YAML or JSON format.
---
- addr: https://facebook.com
weight: 2
- addr: https://twitch.tv
weight: 1
- addr: https://google.com
weight: 3[
{
"addr": "https://facebook.com",
"weight": 2
},
{
"addr": "https://twitch.tv",
"weight": 1
},
{
"addr": "https://google.com",
"weight": 3
}
]Round Robin (rr): Distributes requests evenly across all available servers. Weighted Round Robin (wrr): Distributes requests based on the weight assigned to each server. Servers with higher weights receive more traffic. Local Server Spawning When using -env local, the program spawns a number of local servers on ports starting from 8000 (e.g., localhost:8000, localhost:8001, etc.).
When using -env external with the -path flag, the load balancer reads external server addresses from the specified JSON or YAML file and balances requests accordingly.
Example output when running with 3 local servers:
serving requests at localhost:7000
forwarding to "localhost:8000"
forwarding to "localhost:8001"
forwarding to "localhost:8002"Example output when using an external JSON configuration:
serving requests at localhost:7000
forwarding to "https://facebook.com"
forwarding to "https://twitch.tv"
forwarding to "https://google.com"We can pass -srv-port flag to specify port for local servers and -port flag to specify port for load balancer
./lb -srv-port 8000 -port 7000 We are able to run health check on external servers listed in the config file
./lb -healthCheck -path ./servers.yamlThis project is open-source and available under the MIT License.