You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This Go project implements a simple load balancer that forwards requests to multiple backend servers. The project dynamically spawns HTTP servers and balances the incoming requests using round-robin scheduling.
3
-
4
-
Overview
5
-
The project consists of:
6
-
7
-
A server spawner that creates multiple backend servers listening on different ports.
8
-
A load balancer that receives incoming requests on a designated port and forwards them to one of the spawned servers using a round-robin mechanism.
9
-
Each server responds with a message indicating the port it is serving on.
10
-
Features
11
-
Dynamic server spawning with a given number of servers.
12
-
Load balancing using the round-robin technique.
13
-
Each server runs independently and responds with a simple message.
14
-
Basic reverse proxy functionality for forwarding requests to backend servers.
15
-
Project Structure
16
-
Files
17
-
main.go: The entry point of the application that spawns servers and starts the load balancer.
18
-
loadbalancer.go: Contains the logic for the load balancer and the backend server configurations.
19
-
Code Explanation
20
-
Server Spawner
21
-
The Spawner function spawns a specified number of backend HTTP servers. Each server is assigned a unique port and name. Servers are spawned concurrently in goroutines to avoid blocking the main thread.
22
-
23
-
```go
24
-
funcSpawner(amtint) []LbServer {
25
-
servers:=make([]LbServer, 0, amt)
26
-
fori:=0; i < amt; i++ {
27
-
name:= fmt.Sprintf("Server %v", i)
28
-
srv:=Server(":"+strconv.Itoa(8000+i), name)
29
-
servers = append(servers, srv)
30
-
}
31
-
return servers
32
-
}
33
-
```
34
-
Server Initialization
35
-
Each server is initialized using the Server function. It creates an HTTP server that listens on the given port and serves a simple response message.
36
-
37
-
```go
38
-
funcServer(port, namestring) LbServer {
39
-
srv:=NewLbServer("http://localhost" + port)
40
-
srv.name = name
41
-
// Serve HTTP requests in a goroutine
42
-
gofunc() {
43
-
http.ListenAndServe(port, mux)
44
-
}()
45
-
return *srv
46
-
}
47
-
```
48
-
Load Balancer
49
-
The LoadBalancer struct holds a list of backend servers and distributes incoming requests among them using a round-robin algorithm.
The load balancer listens on a specified port (e.g., 7000), and forwards requests to the next available backend server.
59
-
60
-
```go
61
-
func(lb *LoadBalancer) ServeProxy(whttp.ResponseWriter, r *http.Request) {
62
-
targetServer:= lb.GetNextAvailableServer()
63
-
fmt.Printf("forwarding to %q\n", targetServer.Address())
64
-
targetServer.Serve(w, r)
65
-
}
66
-
```
67
-
Running the Code
68
-
Clone this repository or copy the source files into your Go workspace.
1
+
# Go Load Balancer with Configurable Algorithms and External Server Configuration
69
2
70
-
Run the code using:
3
+
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.
71
4
72
-
```bash
73
-
go build *.go -o ./lb && ./lb
74
-
```
75
-
or
5
+
## Features
76
6
77
-
```bash
78
-
make run
79
-
```
80
-
The load balancer will listen on port 7000. Open your browser or use curl to send a request to localhost:7000:
7
+
-**Round Robin** (`rr`) and **Weighted Round Robin** (`wrr`) algorithms for load balancing.
8
+
- Support for **local server spawning** or **external server configuration** via `.json` or `.yaml` files.
9
+
- Configurable via command-line flags.
10
+
11
+
## Command-Line Flags
12
+
13
+
-`-amount`: Number of local servers to spawn (used only with `-env local`). Default is `1234`.
-`local`: Spawns the specified amount of local servers.
19
+
-`external`: Reads server addresses from an external file (provided via `-path` flag).
20
+
-`-path`: Specifies the path to the external `.json` or `.yaml` configuration file (used with `-env external`).
21
+
22
+
### Example Usage
23
+
24
+
1.**Spawn Local Servers** (5 servers, round-robin method):
25
+
```bash
26
+
go run *.go -amount 5 -method rr -env local
27
+
Use External Servers from a JSON File (weighted round-robin method):
81
28
82
29
```bash
83
-
curl http://localhost:7000
30
+
go run *.go -method wrr -env external -path ./servers.json
84
31
```
85
-
The request will be forwarded to one of the backend servers, and you will see a response indicating the server's port.
32
+
Use External Servers from a YAML File (round-robin method):
86
33
87
-
We can also specify amount of servers and balancing method (Round Robin and Weighted Round Robin) by building program and passing flags:
88
34
```bash
89
-
make build && ./lb -amount 5 -method rr
35
+
go run *.go -method rr -env external -path ./servers.yaml
90
36
```
91
-
Example Output
92
-
```bash Serving requests at localhost:7000
93
-
Spawning server: Server 0 at localhost:8000
94
-
Spawning server: Server 1 at localhost:8001
95
-
Spawning server: Server 2 at localhost:8002
96
-
Spawning server: Server 3 at localhost:8003
97
-
Spawning server: Server 4 at localhost:8004
98
-
forwarding to "localhost:8001"
99
-
forwarding to "localhost:8002"
100
-
forwarding to "localhost:8003"
101
-
forwarding to "localhost:8004"
102
-
forwarding to "localhost:8000"
37
+
### Configuration File Format
38
+
When 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.
39
+
40
+
# Sample YAML Configuration (servers.yaml)
41
+
```yaml
42
+
---
43
+
- addr: https://facebook.com
44
+
weight: 2
45
+
- addr: https://framed-designs.com
46
+
weight: 1
47
+
- addr: https://google.com
48
+
weight: 3
103
49
```
104
-
You can also use the loadbalancer to redirect traffic to external servers. Create <b>servers.json</b> in the root directory
105
-
of the project and specify servers (add weights if you want to use Weighted Round Robin).
50
+
# Sample JSON Configuration (servers.json)
106
51
```json
107
52
[
108
53
{
@@ -119,17 +64,34 @@ of the project and specify servers (add weights if you want to use Weighted Roun
119
64
}
120
65
]
121
66
```
122
-
Then simply pass <b>-env</b> flag. It accepts "local" and "external" where "local" will start local servers
123
-
and "external" will read the JSON file.
67
+
# How It Works
68
+
## Load Balancing Methods
69
+
Round Robin (rr): Distributes requests evenly across all available servers.
70
+
Weighted Round Robin (wrr): Distributes requests based on the weight assigned to each server. Servers with higher weights receive more traffic.
71
+
Local Server Spawning
72
+
When using -env local, the program spawns a number of local servers on ports starting from 8000 (e.g., localhost:8000, localhost:8001, etc.).
73
+
74
+
## External Servers
75
+
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.
76
+
77
+
## Example Output
78
+
Example output when running with 3 local servers:
124
79
125
80
```bash
126
-
make build && ./lb -method wrr -env external
81
+
serving requests at localhost:7000
82
+
forwarding to "localhost:8000"
83
+
forwarding to "localhost:8001"
84
+
forwarding to "localhost:8002"
85
+
```
86
+
Example output when using an external JSON configuration:
87
+
88
+
```bash
89
+
serving requests at localhost:7000
90
+
forwarding to "https://facebook.com"
91
+
forwarding to "https://twitch.tv"
92
+
forwarding to "https://google.com"
127
93
```
128
-
Customization
129
-
Number of Servers: You can modify the number of servers spawned by changing the <b>-amount</b> flag value.
130
-
Ports: The backend servers listen on ports 8000 and higher. You can modify the port range in the Spawner function.
131
-
Dependencies
132
-
This project does not require any third-party dependencies. It only uses the Go standard library.
133
-
134
-
License
135
-
This project is licensed under the MIT License.
94
+
95
+
# License
96
+
This project is open-source and available under the MIT License.
0 commit comments