Last active
January 2, 2020 18:12
-
-
Save furdarius/f12540fadf27409d64cb8cd797b38437 to your computer and use it in GitHub Desktop.
Go Graceful Shutdown with errorgroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where does
TermSignalTrap
come from?