-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.go
113 lines (88 loc) · 1.89 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package bootstrap
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/why444216978/go-util/assert"
"golang.org/x/sync/errgroup"
"github.com/air-go/rpc/library/registry"
"github.com/air-go/rpc/server"
)
type Option struct {
registrar registry.Registrar
}
type OptionFunc func(*Option)
func defaultOption() *Option {
return &Option{}
}
func WithRegistrar(r registry.Registrar) OptionFunc {
return func(o *Option) { o.registrar = r }
}
type App struct {
opt *Option
ctx context.Context
server server.Server
cancel func()
}
func NewApp(srv server.Server, opts ...OptionFunc) *App {
opt := defaultOption()
for _, o := range opts {
o(opt)
}
ctx, cancel := context.WithCancel(context.Background())
app := &App{
opt: opt,
ctx: ctx,
cancel: cancel,
server: srv,
}
return app
}
func (a *App) Start() error {
g, _ := errgroup.WithContext(a.ctx)
g.Go(func() (err error) {
return a.start()
})
g.Go(func() (err error) {
return a.registerSignal()
})
g.Go(func() (err error) {
return a.registerService()
})
g.Go(func() (err error) {
return a.shutdown()
})
return g.Wait()
}
func (a *App) start() error {
return a.server.Start()
}
func (a *App) registerSignal() (err error) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
err = errors.Errorf("%s: exit by signal %v\n", time.Now().Format("2006-01-02 15:04:05"), <-ch)
// trigger shutdown
a.cancel()
return
}
func (a *App) registerService() (err error) {
if assert.IsNil(a.opt.registrar) {
return
}
return a.opt.registrar.Register(a.ctx)
}
func (a *App) shutdown() (err error) {
<-a.ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
// server shutdown
err = a.server.Close()
// clean resource
for _, f := range server.CloseFunc {
_ = f(ctx)
}
return
}