-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
201 lines (169 loc) · 3.86 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package kitx
import (
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/sado0823/go-kitx/kit/registry"
"github.com/sado0823/go-kitx/transport"
"golang.org/x/sync/errgroup"
)
type (
AppI interface {
ID() string
Name() string
Version() string
Metadata() map[string]string
Endpoint() []string
}
App struct {
ctxWithCancel context.Context
opt *option
ctxCancel context.CancelFunc
lock sync.Mutex
registrySvc *registry.Service
}
Option func(o *option)
appKey struct{}
)
func NewContext(ctx context.Context, s AppI) context.Context {
return context.WithValue(ctx, appKey{}, s)
}
func FromContext(ctx context.Context) (s AppI, ok bool) {
s, ok = ctx.Value(appKey{}).(AppI)
return
}
func New(opts ...Option) *App {
opt := &option{
ctx: context.Background(),
signals: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
registrarTimeout: time.Second * 5,
stopTimeout: time.Second * 10,
servers: nil,
}
for _, o := range opts {
o(opt)
}
opt.Fix()
ctx, cancelFunc := context.WithCancel(opt.ctx)
return &App{
ctxWithCancel: ctx,
opt: opt,
ctxCancel: cancelFunc,
}
}
func (a *App) ID() string {
return a.opt.id
}
func (a *App) Name() string {
return a.opt.name
}
func (a *App) Version() string {
return a.opt.version
}
func (a *App) Metadata() map[string]string {
return a.opt.metadata
}
func (a *App) Endpoint() []string {
if a.registrySvc != nil {
return a.registrySvc.Endpoints
}
return []string{}
}
func (a *App) Stop() error {
a.lock.Lock()
svc := a.registrySvc
a.lock.Unlock()
if a.opt.registrar != nil && svc != nil {
ctx, cancel := context.WithTimeout(NewContext(a.ctxWithCancel, a), a.opt.registrarTimeout)
defer cancel()
if err := a.opt.registrar.Deregister(ctx, svc); err != nil {
return err
}
}
if a.ctxCancel != nil {
a.ctxCancel()
}
return nil
}
func (a *App) Run() error {
registrySvc, err := a.genRegistrySvc()
if err != nil {
return err
}
a.lock.Lock()
a.registrySvc = registrySvc
a.lock.Unlock()
startingPrint(a.ID(), a.Name())
var (
eg, ctxWithApp = errgroup.WithContext(NewContext(a.ctxWithCancel, a))
wg sync.WaitGroup
)
for _, server := range a.opt.servers {
server := server
// server stop go-routine
eg.Go(func() error {
<-ctxWithApp.Done() // ctx will be canceled when app stop
stopCtx, stopCancel := context.WithTimeout(NewContext(a.opt.ctx, a), a.opt.stopTimeout)
defer stopCancel()
return server.Stop(stopCtx)
})
wg.Add(1)
// server start go-routine
eg.Go(func() error {
wg.Done()
return server.Start(NewContext(a.opt.ctx, a))
})
}
// wait all server started
wg.Wait()
// use registry
if a.opt.registrar != nil {
regisCtx, regisCancel := context.WithTimeout(ctxWithApp, a.opt.registrarTimeout)
defer regisCancel()
return a.opt.registrar.Register(regisCtx, a.registrySvc)
}
// wait signals for stop
sig := make(chan os.Signal, 1)
signal.Notify(sig, a.opt.signals...)
eg.Go(func() error {
select {
case <-ctxWithApp.Done():
return nil
case <-sig:
return a.Stop()
}
})
err = eg.Wait()
if err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}
func (a *App) genRegistrySvc() (*registry.Service, error) {
endpoints := make([]string, 0, len(a.opt.endpoints))
for _, endpoint := range a.opt.endpoints {
endpoints = append(endpoints, endpoint.String())
}
if len(endpoints) == 0 {
for _, server := range a.opt.servers {
if endpoint, ok := server.(transport.Endpointer); ok {
url, err := endpoint.Endpoint()
if err != nil {
return nil, err
}
endpoints = append(endpoints, url.String())
}
}
}
return ®istry.Service{
ID: a.opt.id,
Name: a.opt.name,
Version: a.opt.version,
Metadata: a.opt.metadata,
Endpoints: endpoints,
}, nil
}