-
Notifications
You must be signed in to change notification settings - Fork 10
/
svc.go
268 lines (233 loc) · 7.17 KB
/
svc.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package svc
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/avast/retry-go/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
const (
defaultTerminationGracePeriod = 15 * time.Second
defaultTerminationWaitPeriod = 0 * time.Second
)
// SVC defines the worker life-cycle manager. It holds service metadata, router,
// logger, and the workers.
type SVC struct {
Name string
Version string
Router *http.ServeMux
TerminationGracePeriod time.Duration
TerminationWaitPeriod time.Duration
signals chan os.Signal
logger *zap.Logger
zapOpts []zap.Option
stdLogger *log.Logger
atom zap.AtomicLevel
loggerRedirectUndo func()
workers map[string]Worker
workerInitRetryOpts map[string][]retry.Option
workersAdded []string
workersInitialized []string
gatherers prometheus.Gatherers
internalRegister *prometheus.Registry
promHander http.Handler
}
// New instantiates a new service by parsing configuration and initializing a
// logger.
func New(name, version string, opts ...Option) (*SVC, error) {
s := &SVC{
Name: name,
Version: version,
Router: http.NewServeMux(),
TerminationGracePeriod: defaultTerminationGracePeriod,
TerminationWaitPeriod: defaultTerminationWaitPeriod,
signals: make(chan os.Signal, 3),
workers: map[string]Worker{},
workersAdded: []string{},
workersInitialized: []string{},
workerInitRetryOpts: map[string][]retry.Option{},
}
if err := WithDevelopmentLogger()(s); err != nil {
return nil, err
}
s.internalRegister = prometheus.NewRegistry()
s.gatherers = []prometheus.Gatherer{s.internalRegister, prometheus.DefaultGatherer}
// Apply options
for _, o := range opts {
if err := o(s); err != nil {
return nil, err
}
}
return s, nil
}
// AddWorker adds a named worker to the service. Added workers order is
// maintained.
func (s *SVC) AddWorker(name string, w Worker) {
if _, exists := s.workers[name]; exists {
s.logger.Fatal("Duplicate worker names!", zap.String("name", name), zap.Stack("stacktrace"))
}
if _, ok := w.(Healther); !ok {
s.logger.Info("Worker does not implement Healther interface", zap.String("worker", name))
}
if _, ok := w.(Aliver); !ok {
s.logger.Info("Worker does not implement Aliver interface", zap.String("worker", name))
}
if g, ok := w.(Gatherer); ok {
s.AddGatherer(g.Gatherer())
} else {
s.logger.Info("Worker does not implement Gatherer interface", zap.String("worker", name))
}
// Track workers as ordered set to initialize them in order.
s.workersAdded = append(s.workersAdded, name)
s.workers[name] = w
}
// AddWorkerWithInitRetry adds a named worker to the service.
// If the worker-initialization fails, it will be retried according to specified options.
func (s *SVC) AddWorkerWithInitRetry(name string, w Worker, retryOpts []retry.Option) {
s.AddWorker(name, w)
s.workerInitRetryOpts[name] = retryOpts
}
func (s *SVC) AddGatherer(gatherer prometheus.Gatherer) {
s.promHander = nil
s.gatherers = append(s.gatherers, gatherer)
}
// Run runs the service until either receiving an interrupt or a worker
// terminates.
func (s *SVC) Run() {
s.logger.Info("Starting up service")
defer func() {
s.logger.Info("Shutting down service", zap.Duration("termination_grace_period", s.TerminationGracePeriod))
s.terminateWorkers()
s.logger.Info("Service shutdown completed")
_ = s.logger.Sync()
s.loggerRedirectUndo()
}()
// Initializing workers in added order.
for _, name := range s.workersAdded {
s.logger.Debug("Initializing worker", zap.String("worker", name))
w := s.workers[name]
var err error
if opts, ok := s.workerInitRetryOpts[name]; ok {
//nolint:scopelint
err = retry.Do(func() error { return w.Init(s.logger.Named(name)) }, opts...)
} else {
err = w.Init(s.logger.Named(name))
}
if err != nil {
s.logger.Error("Could not initialize service", zap.String("worker", name), zap.Error(err))
return
}
s.workersInitialized = append(s.workersInitialized, name)
}
errs := make(chan error)
wg := sync.WaitGroup{}
for name, w := range s.workers {
wg.Add(1)
go func(name string, w Worker) {
defer s.recoverWait(name, &wg, errs)
if err := w.Run(); err != nil {
err = fmt.Errorf("worker %s exited: %w", name, err)
errs <- err
}
}(name, w)
}
signal.Notify(s.signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
select {
case err := <-errs:
if !errors.Is(err, context.Canceled) {
s.logger.Fatal("Worker Init/Run failure", zap.Error(err))
}
s.logger.Warn("Worker context canceled", zap.Error(err))
case sig := <-s.signals:
s.logger.Warn("Caught signal", zap.String("signal", sig.String()))
case <-waitGroupToChan(&wg):
s.logger.Info("All workers have finished")
}
}
// Shutdown signals the framework to terminate any already started workers and
// shutdown the service.
// The call is non-blocking. Terminating the workers comes with the guarantees
// as the `Run` method: All workers are given a total terminate grace-period
// until the service goes ahead completes the shutdown phase.
func (s *SVC) Shutdown() {
s.signals <- syscall.SIGTERM
}
// MustInit is a convenience function to check for and halt on errors.
func MustInit(s *SVC, err error) *SVC {
if err != nil {
if s == nil || s.logger == nil {
panic(err)
}
s.logger.Fatal("Service initialization failed", zap.Error(err), zap.Stack("stacktrace"))
return nil
}
return s
}
// Logger returns the service's logger. Logger might be nil if New() fails.
func (s *SVC) Logger() *zap.Logger {
return s.logger
}
func (s *SVC) terminateWorkers() {
s.logger.Info("Terminating workers down service", zap.Duration("termination_grace_period", s.TerminationGracePeriod))
// terminate only initialized workers
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(s.TerminationWaitPeriod)
for _, name := range s.workersInitialized {
defer func(name string) {
w := s.workers[name]
if err := w.Terminate(); err != nil {
s.logger.Error("Terminated with error",
zap.String("worker", name),
zap.Error(err))
}
s.logger.Info("Worker terminated", zap.String("worker", name))
}(name)
}
}()
waitGroupTimeout(&wg, s.TerminationGracePeriod)
s.logger.Info("All workers terminated")
}
func waitGroupTimeout(wg *sync.WaitGroup, d time.Duration) {
select {
case <-waitGroupToChan(wg):
case <-time.After(d):
}
}
func waitGroupToChan(wg *sync.WaitGroup) <-chan struct{} {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
return c
}
func (s *SVC) recoverWait(name string, wg *sync.WaitGroup, errors chan<- error) {
wg.Done()
if r := recover(); r != nil {
if err, ok := r.(error); ok {
s.logger.Error("recover panic", zap.String("worker", name),
zap.Error(err), zap.Stack("stack"))
errors <- err
} else {
errors <- fmt.Errorf("%v", r)
}
}
}
func (s *SVC) metricsHandler(w http.ResponseWriter, r *http.Request) {
if s.promHander == nil {
s.promHander = promhttp.HandlerFor(s.gatherers, promhttp.HandlerOpts{})
}
s.promHander.ServeHTTP(w, r)
}