@@ -15,31 +15,26 @@ import (
15
15
"time"
16
16
)
17
17
18
- func Start (config * config.Config , e * echo.Echo ) error {
18
+ func Start (ctx context. Context , config * config.Config , e * echo.Echo ) error {
19
19
m := mon .Echo ()
20
20
21
21
registerDefaultRoutes (e )
22
22
e .Use (mon .Middleware ())
23
23
24
- done := make (chan os.Signal , 1 )
25
- signal .Notify (done , os .Interrupt , syscall .SIGTERM )
24
+ return serve (contextWithSigterm (ctx ), e , m , config )
25
+ }
26
+
27
+ func serve (ctx context.Context , e * echo.Echo , p * echo.Echo , config * config.Config ) error {
28
+ g , gCtx := errgroup .WithContext (ctx )
26
29
27
30
go func () {
28
- <- done
29
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
31
+ <- gCtx . Done ()
32
+ shutdownCtx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
30
33
defer cancel ()
31
- _ = e .Shutdown (ctx )
32
- _ = m .Shutdown (ctx )
34
+ _ = e .Shutdown (shutdownCtx )
35
+ _ = p .Shutdown (shutdownCtx )
33
36
}()
34
37
35
- logrus .Infof ("server listening on %s" , config .ListenAddr )
36
- logrus .Infof ("metrics listening on %s" , config .Metrics .ListenAddr )
37
-
38
- return serve (e , m , config )
39
- }
40
-
41
- func serve (e * echo.Echo , p * echo.Echo , config * config.Config ) error {
42
- g := new (errgroup.Group )
43
38
g .Go (func () error {
44
39
if config .Tls .Disable {
45
40
if err := e .Start (config .ListenAddr ); err != nil && ! errors .Is (err , http .ErrServerClosed ) {
@@ -52,12 +47,17 @@ func serve(e *echo.Echo, p *echo.Echo, config *config.Config) error {
52
47
}
53
48
return nil
54
49
})
50
+
55
51
g .Go (func () error {
56
52
if err := p .Start (config .Metrics .ListenAddr ); err != nil && ! errors .Is (err , http .ErrServerClosed ) {
57
53
return err
58
54
}
59
55
return nil
60
56
})
57
+
58
+ logrus .Infof ("server listening on %s" , config .ListenAddr )
59
+ logrus .Infof ("metrics listening on %s" , config .Metrics .ListenAddr )
60
+
61
61
return g .Wait ()
62
62
}
63
63
@@ -66,3 +66,20 @@ func registerDefaultRoutes(e *echo.Echo) {
66
66
return c .Render (http .StatusOK , "index.html" , nil )
67
67
})
68
68
}
69
+
70
+ func contextWithSigterm (ctx context.Context ) context.Context {
71
+ ctxWithCancel , cancel := context .WithCancel (ctx )
72
+ go func () {
73
+ defer cancel ()
74
+
75
+ signalCh := make (chan os.Signal , 1 )
76
+ signal .Notify (signalCh , os .Interrupt , syscall .SIGTERM )
77
+
78
+ select {
79
+ case <- signalCh :
80
+ case <- ctx .Done ():
81
+ }
82
+ }()
83
+
84
+ return ctxWithCancel
85
+ }
0 commit comments