-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (47 loc) · 1.21 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"loadbalancer/balancer"
"loadbalancer/config"
"loadbalancer/utils"
)
func main() {
// Setup logger
logger := log.New(os.Stdout, "load-balancer: ", log.LstdFlags)
// Load configuration and initialize Redis
config, err := config.LoadConfig()
if err != nil {
logger.Fatalf("Error loading configuration: %v\n", err)
}
// Create load balancer
lb := &balancer.LoadBalancer{
Logger: logger,
}
// Add servers from configuration
for _, url := range config.Servers.URLs {
lb.AddServer(url)
}
// Start health checks
healthCheckInterval := time.Duration(config.LoadBalancer.HealthCheckIntervalSeconds) * time.Second
lb.StartHealthChecks(healthCheckInterval)
// Setup graceful shutdown
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-stop
lb.GracefulShutdown()
os.Exit(0)
}()
// Start the load balancer
port := config.LoadBalancer.Port
logger.Println(utils.Colorize(fmt.Sprintf("Load balancer is running on port %d", port), utils.GREEN))
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), lb); err != nil {
logger.Fatalf("Load balancer failed: %v\n", err)
}
}