Skip to content

Commit

Permalink
Merge branch 'list-pagination' of https://github.com/flipt-io/flipt i…
Browse files Browse the repository at this point in the history
…nto list-pagination

* 'list-pagination' of https://github.com/flipt-io/flipt:
  fix: initialize shutdown context timeout after interrupt (#1057)
  • Loading branch information
markphelps committed Oct 5, 2022
2 parents 42750f3 + 4af6767 commit 3158554
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,6 @@ func run(ctx context.Context, logger *zap.Logger) error {
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(interrupt)

shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()

var (
isRelease = isRelease()
isConsole = cfg.Log.Encoding == config.LogEncodingConsole
Expand Down Expand Up @@ -384,6 +381,8 @@ func run(ctx context.Context, logger *zap.Logger) error {
var (
grpcServer *grpc.Server
httpServer *http.Server

shutdownFuncs = []func(context.Context){}
)

// starts grpc server
Expand Down Expand Up @@ -496,7 +495,7 @@ func run(ctx context.Context, logger *zap.Logger) error {
DB: cfg.Cache.Redis.DB,
})

defer rdb.Shutdown(shutdownCtx)
shutdownFuncs = append(shutdownFuncs, func(ctx context.Context) { _ = rdb.Shutdown(ctx) })

status := rdb.Ping(ctx)
if status == nil {
Expand Down Expand Up @@ -533,6 +532,12 @@ func run(ctx context.Context, logger *zap.Logger) error {
// initialize grpc server
grpcServer = grpc.NewServer(grpcOpts...)

// register grpcServer graceful stop on shutdown
shutdownFuncs = append(shutdownFuncs, func(context.Context) {
grpcServer.GracefulStop()
logger.Info("grpc server shutdown gracefully")
})

pb.RegisterFliptServer(grpcServer, srv)
grpc_prometheus.EnableHandlingTimeHistogram()
grpc_prometheus.Register(grpcServer)
Expand Down Expand Up @@ -657,6 +662,12 @@ func run(ctx context.Context, logger *zap.Logger) error {
MaxHeaderBytes: 1 << 20,
}

// register httpServer graceful stop on shutdown
shutdownFuncs = append(shutdownFuncs, func(ctx context.Context) {
_ = httpServer.Shutdown(ctx)
logger.Info("http server shutdown gracefully")
})

logger.Debug("starting http server")

var (
Expand Down Expand Up @@ -703,7 +714,6 @@ func run(ctx context.Context, logger *zap.Logger) error {
return fmt.Errorf("http server: %w", err)
}

logger.Info("server shutdown gracefully")
return nil
})

Expand All @@ -718,12 +728,11 @@ func run(ctx context.Context, logger *zap.Logger) error {

cancel()

if httpServer != nil {
_ = httpServer.Shutdown(shutdownCtx)
}
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()

if grpcServer != nil {
grpcServer.GracefulStop()
for _, shutdown := range shutdownFuncs {
shutdown(shutdownCtx)
}

return g.Wait()
Expand Down

0 comments on commit 3158554

Please sign in to comment.