-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
555 lines (490 loc) · 14.7 KB
/
server.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package keel
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"reflect"
"slices"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/foomo/keel/config"
"github.com/foomo/keel/env"
"github.com/foomo/keel/healthz"
"github.com/foomo/keel/interfaces"
"github.com/foomo/keel/log"
"github.com/foomo/keel/markdown"
"github.com/foomo/keel/metrics"
"github.com/foomo/keel/service"
"github.com/foomo/keel/telemetry"
"github.com/go-logr/logr"
"github.com/spf13/viper"
otelhost "go.opentelemetry.io/contrib/instrumentation/host"
otelruntime "go.opentelemetry.io/contrib/instrumentation/runtime"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
// Server struct
type Server struct {
services []Service
initServices []Service
meter metric.Meter
meterProvider metric.MeterProvider
tracer trace.Tracer
traceProvider trace.TracerProvider
shutdown atomic.Bool
shutdownSignals []os.Signal
// gracefulPeriod should equal the terminationGracePeriodSeconds
gracefulPeriod time.Duration
running atomic.Bool
syncClosers []interface{}
syncClosersLock sync.RWMutex
syncReadmers []interfaces.Readmer
syncReadmersLock sync.RWMutex
syncProbes map[healthz.Type][]interface{}
syncProbesLock sync.RWMutex
ctx context.Context
cancel context.CancelFunc
gracefulCtx context.Context
gracefulCancel context.CancelFunc
g *errgroup.Group
gCtx context.Context
l *zap.Logger
c *viper.Viper
}
func NewServer(opts ...Option) *Server {
inst := &Server{
gracefulPeriod: time.Duration(env.GetInt("KEEL_GRACEFUL_PERIOD", 30)) * time.Second,
shutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM},
syncReadmers: []interfaces.Readmer{},
syncProbes: map[healthz.Type][]interface{}{},
ctx: context.Background(),
c: config.Config(),
l: log.Logger(),
}
for _, opt := range opts {
opt(inst)
}
{ // setup error group
inst.AddReadinessHealthzers(healthz.NewHealthzerFn(func(ctx context.Context) error {
if inst.shutdown.Load() {
return ErrServerShutdown
}
return nil
}))
inst.ctx, inst.cancel = context.WithCancel(inst.ctx)
inst.g, inst.gCtx = errgroup.WithContext(inst.ctx)
inst.gracefulCtx, inst.gracefulCancel = signal.NotifyContext(inst.ctx, inst.shutdownSignals...)
// gracefully shutdown
inst.g.Go(func() error {
<-inst.gracefulCtx.Done()
inst.shutdown.Store(true)
timeoutCtx, timeoutCancel := context.WithTimeout(inst.ctx, inst.gracefulPeriod)
defer timeoutCancel()
inst.l.Info("keel graceful shutdown",
zap.Duration("graceful_period", inst.gracefulPeriod),
)
// append internal closers
closers := append(inst.closers(), inst.traceProvider, inst.meterProvider)
inst.l.Info("keel graceful shutdown: closers")
for _, closer := range closers {
var err error
l := inst.l.With(log.FName(fmt.Sprintf("%T", closer)))
switch c := closer.(type) {
case interfaces.Closer:
c.Close()
case interfaces.ErrorCloser:
err = c.Close()
case interfaces.CloserWithContext:
c.Close(timeoutCtx)
case interfaces.ErrorCloserWithContext:
err = c.Close(timeoutCtx)
case interfaces.Shutdowner:
c.Shutdown()
case interfaces.ErrorShutdowner:
err = c.Shutdown()
case interfaces.ShutdownerWithContext:
c.Shutdown(timeoutCtx)
case interfaces.ErrorShutdownerWithContext:
err = c.Shutdown(timeoutCtx)
case interfaces.Stopper:
c.Stop()
case interfaces.ErrorStopper:
err = c.Stop()
case interfaces.StopperWithContext:
c.Stop(timeoutCtx)
case interfaces.ErrorStopperWithContext:
err = c.Stop(timeoutCtx)
case interfaces.Unsubscriber:
c.Unsubscribe()
case interfaces.ErrorUnsubscriber:
err = c.Unsubscribe()
case interfaces.UnsubscriberWithContext:
c.Unsubscribe(timeoutCtx)
case interfaces.ErrorUnsubscriberWithContext:
err = c.Unsubscribe(timeoutCtx)
}
if err != nil {
l.Warn("keel graceful shutdown: closer failed", zap.Error(err))
} else {
l.Debug("keel graceful shutdown: closer closed")
}
}
inst.l.Info("keel graceful shutdown: complete")
return ErrServerShutdown
})
}
{ // setup telemetry
var err error
otel.SetLogger(logr.New(telemetry.NewLogger(inst.l)))
otel.SetErrorHandler(telemetry.NewErrorHandler(inst.l))
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
if inst.meterProvider == nil {
inst.meterProvider, err = telemetry.NewNoopMeterProvider()
log.Must(inst.l, err, "failed to create meter provider")
} else if env.GetBool("OTEL_ENABLED", false) {
if env.GetBool("OTEL_METRICS_HOST_ENABLED", false) {
log.Must(inst.l, otelhost.Start(), "failed to start otel host metrics")
}
if env.GetBool("OTEL_METRICS_RUNTIME_ENABLED", false) {
log.Must(inst.l, otelruntime.Start(), "failed to start otel runtime metrics")
}
}
inst.meter = telemetry.Meter()
if inst.traceProvider == nil {
inst.traceProvider, err = telemetry.NewNoopTraceProvider()
log.Must(inst.l, err, "failed to create tracer provider")
}
inst.tracer = telemetry.Tracer()
}
// add probe
inst.AddAlwaysHealthzers(inst)
inst.AddReadmers(
interfaces.ReadmeFunc(env.Readme),
interfaces.ReadmeFunc(config.Readme),
inst,
interfaces.ReadmeFunc(metrics.Readme),
)
// start init services
inst.startService(inst.initServices...)
return inst
}
// Logger returns server logger
func (s *Server) Logger() *zap.Logger {
return s.l
}
// Meter returns the implementation meter
func (s *Server) Meter() metric.Meter {
return s.meter
}
// Tracer returns the implementation tracer
func (s *Server) Tracer() trace.Tracer {
return s.tracer
}
// Config returns server config
func (s *Server) Config() *viper.Viper {
return s.c
}
// Context returns server context
func (s *Server) Context() context.Context {
return s.ctx
}
// ShutdownContext returns server's shutdown cancel context
func (s *Server) ShutdownContext() context.Context {
return s.gracefulCtx
}
// ShutdownCancel returns server's shutdown cancel function
func (s *Server) ShutdownCancel() context.CancelFunc {
return s.gracefulCancel
}
// AddService add a single service
func (s *Server) AddService(v Service) {
if !slices.Contains(s.services, v) {
s.services = append(s.services, v)
s.AddAlwaysHealthzers(v)
s.AddCloser(v)
}
}
// AddServices adds multiple service
func (s *Server) AddServices(services ...Service) {
for _, value := range services {
s.AddService(value)
}
}
// AddCloser adds a closer to be called on shutdown
func (s *Server) AddCloser(closer interface{}) {
for _, value := range s.closers() {
if value == closer {
return
}
}
if IsCloser(closer) {
s.addClosers(closer)
} else {
s.l.Warn("unable to add closer", log.FValue(fmt.Sprintf("%T", closer)))
}
}
// AddClosers adds the given closers to be called on shutdown
func (s *Server) AddClosers(closers ...interface{}) {
for _, closer := range closers {
s.AddCloser(closer)
}
}
// AddReadmer adds a readmer to be added to the exposed readme
func (s *Server) AddReadmer(readmer interfaces.Readmer) {
s.addReadmers(readmer)
}
// AddReadmers adds readmers to be added to the exposed readme
func (s *Server) AddReadmers(readmers ...interfaces.Readmer) {
for _, readmer := range readmers {
s.AddReadmer(readmer)
}
}
// AddHealthzer adds a probe to be called on healthz checks
func (s *Server) AddHealthzer(typ healthz.Type, probe interface{}) {
if IsHealthz(probe) {
s.addProbes(typ, probe)
} else {
s.l.Debug("not a healthz probe", log.FValue(fmt.Sprintf("%T", probe)))
}
}
// AddHealthzers adds the given probes to be called on healthz checks
func (s *Server) AddHealthzers(typ healthz.Type, probes ...interface{}) {
for _, probe := range probes {
s.AddHealthzer(typ, probe)
}
}
// AddAlwaysHealthzers adds the probes to be called on any healthz checks
func (s *Server) AddAlwaysHealthzers(probes ...interface{}) {
s.AddHealthzers(healthz.TypeAlways, probes...)
}
// AddStartupHealthzers adds the startup probes to be called on healthz checks
func (s *Server) AddStartupHealthzers(probes ...interface{}) {
s.AddHealthzers(healthz.TypeStartup, probes...)
}
// AddLivenessHealthzers adds the liveness probes to be called on healthz checks
func (s *Server) AddLivenessHealthzers(probes ...interface{}) {
s.AddHealthzers(healthz.TypeLiveness, probes...)
}
// AddReadinessHealthzers adds the readiness probes to be called on healthz checks
func (s *Server) AddReadinessHealthzers(probes ...interface{}) {
s.AddHealthzers(healthz.TypeReadiness, probes...)
}
// Healthz returns true if the server is running
func (s *Server) Healthz() error {
if !s.running.Load() {
return ErrServerNotRunning
}
return nil
}
// Run runs the server
func (s *Server) Run() {
s.l.Info("starting keel server")
defer s.cancel()
// start services
s.startService(s.services...)
// add init services to closers
for _, initService := range s.initServices {
s.AddClosers(initService)
}
// set running
defer s.running.Store(false)
s.running.Store(true)
// wait for shutdown
if err := s.g.Wait(); errors.Is(err, ErrServerShutdown) {
s.l.Info("keel server stopped")
} else if err != nil {
log.WithError(s.l, err).Error("keel server failed")
}
}
func (s *Server) closers() []interface{} {
s.syncClosersLock.RLock()
defer s.syncClosersLock.RUnlock()
return s.syncClosers
}
func (s *Server) addClosers(v ...interface{}) {
s.syncClosersLock.Lock()
defer s.syncClosersLock.Unlock()
s.syncClosers = append(s.syncClosers, v...)
}
func (s *Server) readmers() []interfaces.Readmer {
s.syncReadmersLock.RLock()
defer s.syncReadmersLock.RUnlock()
return s.syncReadmers
}
func (s *Server) addReadmers(v ...interfaces.Readmer) {
s.syncReadmersLock.Lock()
defer s.syncReadmersLock.Unlock()
s.syncReadmers = append(s.syncReadmers, v...)
}
func (s *Server) probes() map[healthz.Type][]interface{} {
s.syncProbesLock.RLock()
defer s.syncProbesLock.RUnlock()
return s.syncProbes
}
func (s *Server) addProbes(typ healthz.Type, v ...interface{}) {
s.syncProbesLock.Lock()
defer s.syncProbesLock.Unlock()
s.syncProbes[typ] = append(s.syncProbes[typ], v...)
}
// Readme returns the self-documenting string
func (s *Server) Readme() string {
md := &markdown.Markdown{}
md.Println(s.readmeServices())
md.Println(s.readmeHealthz())
md.Print(s.readmeCloser())
return md.String()
}
// ------------------------------------------------------------------------------------------------
// ~ Private methods
// ------------------------------------------------------------------------------------------------
// startService starts the given services
func (s *Server) startService(services ...Service) {
c := make(chan struct{}, 1)
for _, value := range services {
s.g.Go(func() error {
c <- struct{}{}
if err := value.Start(s.ctx); errors.Is(err, http.ErrServerClosed) {
log.WithError(s.l, err).Debug("server has closed")
} else if err != nil {
log.WithError(s.l, err).Error("failed to start service")
return err
}
return nil
})
<-c
}
close(c)
}
func (s *Server) readmeCloser() string {
md := &markdown.Markdown{}
closers := s.closers()
rows := make([][]string, 0, len(closers))
for _, value := range closers {
t := reflect.TypeOf(value)
var closer string
switch value.(type) {
case interfaces.Closer:
closer = "Closer"
case interfaces.ErrorCloser:
closer = "ErrorCloser"
case interfaces.CloserWithContext:
closer = "CloserWithContext"
case interfaces.ErrorCloserWithContext:
closer = "ErrorCloserWithContext"
case interfaces.Shutdowner:
closer = "Shutdowner"
case interfaces.ErrorShutdowner:
closer = "ErrorShutdowner"
case interfaces.ShutdownerWithContext:
closer = "ShutdownerWithContext"
case interfaces.ErrorShutdownerWithContext:
closer = "ErrorShutdownerWithContext"
case interfaces.Stopper:
closer = "Stopper"
case interfaces.ErrorStopper:
closer = "ErrorStopper"
case interfaces.StopperWithContext:
closer = "StopperWithContext"
case interfaces.ErrorStopperWithContext:
closer = "ErrorStopperWithContext"
case interfaces.Unsubscriber:
closer = "Unsubscriber"
case interfaces.ErrorUnsubscriber:
closer = "ErrorUnsubscriber"
case interfaces.UnsubscriberWithContext:
closer = "UnsubscriberWithContext"
case interfaces.ErrorUnsubscriberWithContext:
closer = "ErrorUnsubscriberWithContext"
}
rows = append(rows, []string{
markdown.Code(markdown.Name(value)),
markdown.Code(t.String()),
markdown.Code(closer),
markdown.String(value),
})
}
if len(rows) > 0 {
md.Println("### Closers")
md.Println("")
md.Println("List of all registered closers that are being called during graceful shutdown.")
md.Println("")
md.Table([]string{"Name", "Type", "Closer", "Description"}, rows)
md.Println("")
}
return md.String()
}
func (s *Server) readmeHealthz() string {
var rows [][]string
md := &markdown.Markdown{}
for k, probes := range s.probes() {
for _, probe := range probes {
t := reflect.TypeOf(probe)
rows = append(rows, []string{
markdown.Code(markdown.Name(probe)),
markdown.Code(k.String()),
markdown.Code(t.String()),
markdown.String(probe),
})
}
}
if len(rows) > 0 {
md.Println("### Health probes")
md.Println("")
md.Println("List of all registered healthz probes that are being called during startup and runtime.")
md.Println("")
md.Table([]string{"Name", "Probe", "Type", "Description"}, rows)
}
return md.String()
}
func (s *Server) readmeServices() string {
md := &markdown.Markdown{}
{
var rows [][]string
for _, value := range s.initServices {
if v, ok := value.(*service.HTTP); ok {
t := reflect.TypeOf(v)
rows = append(rows, []string{
markdown.Code(v.Name()),
markdown.Code(t.String()),
markdown.String(v),
})
}
}
if len(rows) > 0 {
md.Println("### Init Services")
md.Println("")
md.Println("List of all registered init services that are being immediately started.")
md.Println("")
md.Table([]string{"Name", "Type", "Address"}, rows)
}
}
md.Println("")
{
var rows [][]string
for _, value := range s.services {
t := reflect.TypeOf(value)
rows = append(rows, []string{
markdown.Code(value.Name()),
markdown.Code(t.String()),
markdown.String(value),
})
}
if len(rows) > 0 {
md.Println("### Runtime Services")
md.Println("")
md.Println("List of all registered services that are being started.")
md.Println("")
md.Table([]string{"Name", "Type", "Description"}, rows)
}
}
return md.String()
}