feat: add closers

This commit is contained in:
franklin 2021-09-29 13:33:56 +02:00
parent e6e1ca1bed
commit 6c0c150512
2 changed files with 63 additions and 7 deletions

View File

@ -4,30 +4,60 @@ import "context"
// Closer interface
type Closer interface {
Close()
}
// ErrorCloser interface
type ErrorCloser interface {
Close() error
}
// CloserWithContext interface
type CloserWithContext interface {
Close(ctx context.Context)
}
// ErrorCloserWithContext interface
type ErrorCloserWithContext interface {
Close(ctx context.Context) error
}
// Shutdowner interface
type Shutdowner interface {
Shutdown()
}
// ErrorShutdowner interface
type ErrorShutdowner interface {
Shutdown() error
}
// ShutdownerWithContext interface
type ShutdownerWithContext interface {
Shutdown(ctx context.Context)
}
// ErrorShutdownerWithContext interface
type ErrorShutdownerWithContext interface {
Shutdown(ctx context.Context) error
}
// Unsubscriber interface
type Unsubscriber interface {
Unsubscribe()
}
// ErrorUnsubscriber interface
type ErrorUnsubscriber interface {
Unsubscribe() error
}
// UnsubscriberWithContext interface
type UnsubscriberWithContext interface {
Unsubscribe(ctx context.Context)
}
// ErrorUnsubscriberWithContext interface
type ErrorUnsubscriberWithContext interface {
Unsubscribe(ctx context.Context) error
}

View File

@ -2,6 +2,7 @@ package keel
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
@ -92,11 +93,17 @@ func (s *Server) AddClosers(closers ...interface{}) {
func (s *Server) AddCloser(closer interface{}) {
switch closer.(type) {
case Closer,
ErrorCloser,
CloserWithContext,
ErrorCloserWithContext,
Shutdowner,
ErrorShutdowner,
ShutdownerWithContext,
ErrorShutdownerWithContext,
Unsubscriber,
UnsubscriberWithContext:
ErrorUnsubscriber,
UnsubscriberWithContext,
ErrorUnsubscriberWithContext:
s.closers = append(s.closers, closer)
default:
s.l.Warn("unable to add closer")
@ -144,30 +151,49 @@ func (s *Server) Run() {
for _, closer := range closers {
switch c := closer.(type) {
case Closer:
c.Close()
case ErrorCloser:
if err := c.Close(); err != nil {
log.WithError(s.l, err).Error("failed to gracefully stop Closer")
log.WithError(s.l, err).Error("failed to gracefully stop ErrorCloser")
continue
}
case CloserWithContext:
c.Close(timeoutCtx)
case ErrorCloserWithContext:
if err := c.Close(timeoutCtx); err != nil {
log.WithError(s.l, err).Error("failed to gracefully stop CloserWithContext")
log.WithError(s.l, err).Error("failed to gracefully stop ErrorCloserWithContext")
continue
}
case Shutdowner:
c.Shutdown()
case ErrorShutdowner:
if err := c.Shutdown(); err != nil {
log.WithError(s.l, err).Error("failed to gracefully stop Shutdowner")
log.WithError(s.l, err).Error("failed to gracefully stop ErrorShutdowner")
continue
}
case ShutdownerWithContext:
c.Shutdown(timeoutCtx)
case ErrorShutdownerWithContext:
if err := c.Shutdown(timeoutCtx); err != nil {
log.WithError(s.l, err).Error("failed to gracefully stop ShutdownerWithContext")
log.WithError(s.l, err).Error("failed to gracefully stop ErrorShutdownerWithContext")
continue
}
case Unsubscriber:
c.Unsubscribe()
case ErrorUnsubscriber:
if err := c.Unsubscribe(); err != nil {
log.WithError(s.l, err).Error("failed to gracefully stop Unsubscriber")
log.WithError(s.l, err).Error("failed to gracefully stop ErrorUnsubscriber")
continue
}
case UnsubscriberWithContext:
c.Unsubscribe(timeoutCtx)
case ErrorUnsubscriberWithContext:
if err := c.Unsubscribe(timeoutCtx); err != nil {
log.WithError(s.l, err).Error("failed to gracefully stop UnsubscriberWithContext")
log.WithError(s.l, err).Error("failed to gracefully stop ErrorUnsubscriberWithContext")
continue
}
}
s.l.Info("stopped registered closer", log.FName(fmt.Sprintf("%T", closer)))
}
return gctx.Err()
})