use signal.NotifyContext when shutting down

This commit is contained in:
sentriz
2024-04-01 22:10:30 +01:00
parent fc934c3653
commit 99233ec318

View File

@@ -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")()
@@ -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 {
@@ -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)
}