Skip to content

Instantly share code, notes, and snippets.

@furdarius
Last active January 2, 2020 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furdarius/f12540fadf27409d64cb8cd797b38437 to your computer and use it in GitHub Desktop.
Save furdarius/f12540fadf27409d64cb8cd797b38437 to your computer and use it in GitHub Desktop.
Go Graceful Shutdown with errorgroup
sigTrap := shutdown.TermSignalTrap()
g, ctx := errgroup.WithContext(context.Background())
s := &http.Server{...}
go func() {
err := s.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalf("httpServer error: %v.", err.Error())
}
}()
g.Go(func() error {
<-ctx.Done()
// FIXME: Тут возможно стоит делать новый context и давать время на завершение соединений в статусе idle
return s.Shutdown(ctx)
})
g.Go(startRMQConsumer1(ctx))
g.Go(startRMQConsumer2(ctx))
g.Go(startRMQPublisher(ctx))
// Когда от OS прилетит SIGTERM, все остальные компоненты получат сигнал через канал ctx.Done()
g.Go(func() error {
return sigTrap.Wait(ctx)
})
if err := g.Wait(); err != nil && err != shutdown.ErrTermSig {
log.Fatalf("failed to wait goroutine group: %v.", err.Error())
}
log.Info("graceful shutdown successfully finished")
@StevenACoffman
Copy link

Where does TermSignalTrap come from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment