-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (94 loc) · 3.44 KB
/
main.go
File metadata and controls
117 lines (94 loc) · 3.44 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"fmt"
// "log"
"os"
"os/signal"
"strconv"
"time"
"github.com/saketV8/orbit-go/internal/backend"
"github.com/saketV8/orbit-go/internal/config"
"github.com/saketV8/orbit-go/internal/healthcheck"
"github.com/saketV8/orbit-go/internal/logger"
"github.com/saketV8/orbit-go/internal/router"
"github.com/saketV8/orbit-go/internal/server"
"github.com/saketV8/orbit-go/internal/strategy"
"github.com/saketV8/orbit-go/internal/utils"
)
// my custom logger
var log = logger.NewLog("[Orbit Go]")
func main() {
fmt.Println(utils.AsciiArtOrbitGo)
// initing the config
cfg, err := config.LoadConfig("./config.yaml")
if err != nil {
log.Fatal(err)
}
utils.PrintCurrentConfig(cfg)
// step 1: create server pool
serverpool := backend.NewServerPool()
// step 2: adding the individual backend in the server pool
// getting from config
for _, b := range cfg.Backends {
serverpool.AddBackend(backend.NewBackendWithWeight(b.URL, b.Weight))
}
// step 3: choosing the strategy to redirect the request
// eg. round-robbin, least-connection, IP-Hash, random
// getting from config.yaml
strat := strategy.SelectStrategy(cfg)
// init the server, this is for dependency injection :)
server := &server.Server{
ServerPool: serverpool,
Strategy: strat,
Config: cfg,
}
// setup the route, this will be handler of our server
mux := router.SetupRouter(server)
// setup the HTTP server
server.SetupHTTPServer(mux)
// ======================================================================
// implementing the gracefull shutdown -_-
// ======================================================================
// init the context that lsiten to the interupt
ctxOs, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// this cancel can be called via
// 1. ctxOs ==> on OS interupt
// 3. when I mannually called the cancel
ctx, cancel := context.WithCancel(ctxOs)
defer cancel()
// starting the server in goroutine, so that it doesn't block the main code below it
go func() {
// Start the http server with the config setup from the <SetupHTTPServer>
var PORT string = ":" + strconv.Itoa(cfg.Server.Port)
log.Printf(logger.Cyan+"Load Balancer started at "+logger.Blue+"http://localhost%v\n"+logger.Reset, PORT)
err := server.StartHTTPServer()
if err != nil {
log.Printf(logger.Red+"HTTP server ERROR: %v\n"+logger.Reset, err)
// this ctx cancel(), will triggered the ctx passed in healthchecker
// and also execute the code below <- ctx.Done()
cancel()
}
}()
// start health checker
healthcheck.StartHealthCheck(ctx, server)
log.Println("[HEALTH CHECK] Started")
// below this everthing is blocked till it get executed
<-ctx.Done()
// after ctx.Done() executed
// this should be max time, so that any request below it always get processed and server shutdown gracefully
const DefaultContextTimeout = 5
// new context to call shutdown when the <DefaultContextTimeout> passed
ctxTime, cancelCtxTime := context.WithTimeout(context.Background(), DefaultContextTimeout*time.Second)
defer cancelCtxTime()
// passing this ctx to shutdown, and server will shutdown when ctx.Done()
err = server.ShutDownHTTPServer(ctxTime)
if err != nil {
log.Fatal(logger.Red + "Server forced to shutdown!!" + logger.Reset)
}
// I think we don't need this, as we are using the defer already, but anyway
// stop()
// cancel()
log.Println(logger.Green + "Server shutdown gracefully" + logger.Reset)
}