-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (76 loc) · 2.22 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/abhikvarma/load-balancer/backend"
"github.com/abhikvarma/load-balancer/loadbalancer"
"github.com/abhikvarma/load-balancer/serverpool"
"github.com/abhikvarma/load-balancer/utils"
"github.com/charmbracelet/log"
)
func main() {
config, err := utils.GetLBConfig()
if err != nil {
log.Fatal(err.Error())
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
sp, err := serverpool.NewServerPool(utils.GetLBStrategy(config.Strategy))
if err != nil {
log.Fatal(err.Error())
}
lb := loadbalancer.NewLoadBalancer(sp)
for _, b := range config.Backends {
endpoint, err := url.Parse(b)
if err != nil {
log.Fatal(err.Error())
}
rp := httputil.NewSingleHostReverseProxy(endpoint)
backendServer := backend.NewBackend(endpoint, rp)
rp.ErrorHandler = func(w http.ResponseWriter, r *http.Request, e error) {
log.Info(fmt.Sprintf("error handling the request %s", r.RequestURI), "host", endpoint.Host, e)
attempts, allow := loadbalancer.AllowRetry(r, config.MaxRetries)
if !allow {
log.Info(
"max retry attempts reached, terminating", "address", r.RemoteAddr, "path", r.URL.Path,
)
http.Error(w, "Service not available", http.StatusServiceUnavailable)
return
}
log.Info(
"Attempting retry", "address", r.RemoteAddr, "path", r.URL.Path, "reties", attempts,
)
lb.Serve(
w,
r.WithContext(context.WithValue(r.Context(), loadbalancer.RETRIES_ATTEMPTED, attempts+1)),
)
}
sp.AddBackend(backendServer)
}
server := http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Handler: http.HandlerFunc(lb.Serve),
}
go lb.LaunchHealthCheck(ctx, config.HealthCheckIntervalInSec)
go func() {
<-ctx.Done()
shutdownCtx, stop := context.WithTimeout(context.Background(), 10*time.Second)
defer stop()
if err := server.Shutdown(shutdownCtx); err != nil {
log.Fatal("Error in shutdown", err)
}
}()
log.Info(
"load balancer started", "strategy", config.Strategy, "port", config.Port,
)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal("ListenAndServe() error", err)
}
}