-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (59 loc) · 1.52 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"log"
"lol-balancer/pkg/backend"
serverpool "lol-balancer/pkg/server_pool"
"net/http"
"net/url"
"time"
)
const (
server1 = "http://localhost:3000"
server2 = "http://localhost:3001"
port = ":2205"
)
func main() {
server_urls := []string{server1, server2}
backnedServers := []*backend.Backend{}
for _, server_url := range server_urls {
url, err := url.Parse(server_url)
if err != nil {
log.Fatal(err)
}
backnedServer := backend.NewBackendServer(url)
backnedServers = append(backnedServers, backnedServer)
}
serverpool := serverpool.NewServerPool(backnedServers)
go healthCheck(serverpool)
server := http.Server{
Addr: port,
Handler: http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
loadBalancer(w, r, serverpool)
},
),
}
log.Println("Load balancer started")
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func loadBalancer(w http.ResponseWriter, r *http.Request, serverpool *serverpool.ServerPool) {
peer := serverpool.GetNextPeer()
if peer != nil {
peer.ReverseProxy.ServeHTTP(w, r)
return
}
http.Error(w, "Service not available", http.StatusServiceUnavailable)
}
// * A passive health check where we check the health of the backends every 20 seconds
func healthCheck(serverpool *serverpool.ServerPool) {
// initial health check
serverpool.HealthCheck()
t := time.NewTicker(time.Second * 20)
for range t.C {
log.Println("Starting health check...")
serverpool.HealthCheck()
log.Println("Health check completed")
}
}