-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (96 loc) · 2.56 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"errors"
"github.com/danthegoodman1/Gildra/control_plane"
"github.com/danthegoodman1/Gildra/gologger"
"github.com/danthegoodman1/Gildra/http_server"
"github.com/danthegoodman1/Gildra/internal"
"github.com/danthegoodman1/Gildra/observability"
"github.com/danthegoodman1/Gildra/tracing"
"github.com/danthegoodman1/Gildra/utils"
"go.opentelemetry.io/otel/sdk/trace"
"golang.org/x/sync/errgroup"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
logger = gologger.NewLogger()
)
func main() {
mainCtx := context.Background()
logger.Info().Msg("starting Gildra")
g := errgroup.Group{}
g.Go(func() error {
logger.Debug().Msg("starting proxy servers")
return http_server.StartServers()
})
g.Go(func() error {
logger.Debug().Msg("starting internal server")
return internal.StartServer()
})
if utils.CacheEnabled {
g.Go(func() error {
logger.Debug().Msg("starting groupcache")
control_plane.InitCache(mainCtx)
return nil
})
}
var tracer *trace.TracerProvider
if utils.Env_TracingEnabled {
logger.Info().Msg("enabling tracing")
g.Go(func() (err error) {
tracer, err = tracing.InitTracer(mainCtx)
return
})
}
g.Go(func() error {
prometheusReporter := observability.NewPrometheusReporter()
err := observability.StartInternalHTTPServer(":8042", prometheusReporter)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
err := g.Wait()
if err != nil {
logger.Error().Err(err).Msg("Error starting services")
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
logger.Info().Msg("received shutdown signal!")
// Provide time for load balancers to de-register before stopping accepting connections
if utils.Env_SleepSeconds > 0 {
logger.Info().Msgf("sleeping for %ds before exiting", utils.Env_SleepSeconds)
time.Sleep(time.Second * time.Duration(utils.Env_SleepSeconds))
logger.Info().Msgf("slept for %ds, exiting", utils.Env_SleepSeconds)
}
ctx, cancel := context.WithTimeout(mainCtx, time.Second*time.Duration(utils.Env_SleepSeconds))
defer cancel()
g = errgroup.Group{}
g.Go(func() error {
return http_server.Shutdown(ctx)
})
g.Go(func() error {
return internal.Shutdown(ctx)
})
if tracer != nil {
g.Go(func() error {
return tracer.Shutdown(mainCtx)
})
}
if utils.CacheEnabled {
g.Go(func() error {
return control_plane.StopCache(ctx)
})
}
if err := g.Wait(); err != nil {
logger.Error().Err(err).Msg("error shutting down servers")
os.Exit(1)
}
logger.Info().Msg("shutdown servers, exiting")
}