-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
99 lines (88 loc) · 1.99 KB
/
app.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
package mago
import (
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/cloudwego/hertz/pkg/common/hlog"
"golang.org/x/sync/errgroup"
)
type App struct {
Name string
ctx context.Context
cancel context.CancelFunc
Servers []Server
stopTimeout time.Duration
stopErr error
}
type StopError struct {
}
func New(name string, servers []Server) *App {
ctx, cancel := context.WithCancel(context.Background())
return &App{
Name: name,
Servers: servers,
ctx: ctx,
cancel: cancel,
stopTimeout: time.Second * 5,
}
}
func (a *App) Run() error {
eg, ctx := errgroup.WithContext(a.ctx)
wg := sync.WaitGroup{}
for _, srv := range a.Servers {
if srv == nil {
continue
}
srv := srv
eg.Go(func() error {
<-ctx.Done() // wait for stop signal
stopCtx, cancel := context.WithTimeout(context.WithValue(a.ctx, StopError{}, a.stopErr), a.stopTimeout)
defer cancel()
return srv.Stop(stopCtx)
})
wg.Add(1)
eg.Go(func() error {
wg.Done() // here is to ensure server start has begun running before register, so defer is not needed
return srv.Start(a.ctx)
})
}
wg.Wait()
signalToNotify := []os.Signal{syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM}
if signal.Ignored(syscall.SIGHUP) {
signalToNotify = []os.Signal{syscall.SIGINT, syscall.SIGTERM}
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, signalToNotify...)
eg.Go(func() error {
select {
case <-ctx.Done():
return nil
case sig := <-signals:
return a.Stop(sig)
}
})
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}
func (a *App) Stop(sig os.Signal) error {
if a.cancel != nil {
defer a.cancel()
}
switch sig {
case syscall.SIGTERM:
// force exit
a.stopErr = errors.New(sig.String())
return a.stopErr // nolint
case syscall.SIGHUP, syscall.SIGINT:
hlog.SystemLogger().Infof("Received signal: %s\n", sig)
// graceful shutdown
return nil
}
return nil
}