-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (39 loc) · 943 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus/promhttp"
"load-balancer/lb"
"log"
"net/http"
"os"
"strconv"
"time"
)
func main() {
if len(os.Args) < 3 {
log.Fatal("Usage: main <strategy> <weights>")
}
strategy, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal("Invalid strategy")
}
weights := []int{}
for _, w := range os.Args[2:] {
weight, err := strconv.Atoi(w)
if err != nil {
log.Fatal("Invalid weight")
}
weights = append(weights, weight)
}
servers := []string{
"http://localhost:8081",
"http://localhost:8082",
"http://localhost:8083",
}
rateLimiter := lb.NewRateLimiter(10, 1, time.Second)
loadBalancer := lb.NewLoadBalancer(servers, rateLimiter, lb.Strategy(strategy), weights)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/", loadBalancer)
fmt.Println("Load Balancer started at :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}