Skip to content

Commit

Permalink
use signal.NotifyContext when shutting down
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Apr 1, 2024
1 parent fc934c3 commit 99233ec
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions cmd/gonic/gonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ func main() {
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

errgrp, ctx := errgroup.WithContext(context.Background())
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
errgrp, ctx := errgroup.WithContext(ctx)

errgrp.Go(func() error {
defer logJob("http")()
Expand All @@ -315,7 +317,10 @@ func main() {
if *confTLSCert != "" && *confTLSKey != "" {
return server.ListenAndServeTLS(*confTLSCert, *confTLSKey)
}
return server.ListenAndServe()
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})

errgrp.Go(func() error {
Expand Down Expand Up @@ -439,20 +444,7 @@ func main() {
return nil
})

errShutdown := errors.New("shutdown")

errgrp.Go(func() error {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
return nil
case <-sigChan:
return errShutdown
}
})

if err := errgrp.Wait(); err != nil && !errors.Is(err, errShutdown) {
if err := errgrp.Wait(); err != nil {
log.Panic(err)
}

Expand Down

0 comments on commit 99233ec

Please sign in to comment.