Skip to content

Commit

Permalink
Simplify signal handling logic
Browse files Browse the repository at this point in the history
Since Go 1.16, a signal-stoppable context can be created natively by the `signal` package.
  • Loading branch information
hhromic authored Dec 14, 2022
1 parent 85993ab commit 7db0042
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,9 @@ func main() {

logger.Info("started kminion", zap.String("version", version), zap.String("built_at", builtAt))

// Setup context that cancels when the application receives an interrupt signal
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
logger.Info("received a signal, going to shut down KMinion")
cancel()
case <-ctx.Done():
}
}()
// Setup context that stops when the application receives an interrupt signal
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

wrappedRegisterer := promclient.WrapRegistererWithPrefix(cfg.Exporter.Namespace+"_", promclient.DefaultRegisterer)

Expand Down Expand Up @@ -138,4 +125,6 @@ func main() {
logger.Error("error starting HTTP server", zap.Error(err))
os.Exit(1)
}

logger.Info("kminion stopped")
}

0 comments on commit 7db0042

Please sign in to comment.