Handle SIGTERM on unix-like OS (#1805)

This commit is contained in:
Rob Prentiss
2023-01-24 18:04:56 -08:00
committed by GitHub
parent a1553999bb
commit 6baf7c9033
3 changed files with 29 additions and 2 deletions
+4 -2
View File
@@ -13,6 +13,7 @@ import (
_ "net/http/pprof"
"github.com/gomods/athens/cmd/proxy/actions"
"github.com/gomods/athens/internal/shutdown"
"github.com/gomods/athens/pkg/build"
"github.com/gomods/athens/pkg/config"
)
@@ -51,8 +52,9 @@ func main() {
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
signal.Notify(sigint, shutdown.GetSignals()...)
s := <-sigint
log.Printf("Recived signal (%s): Shutting down server", s)
// We received an interrupt signal, shut down.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(conf.ShutdownTimeout))
+15
View File
@@ -0,0 +1,15 @@
//go:build unix
package shutdown
import (
"os"
"syscall"
)
// GetSignals returns the appropriate signals to catch for a clean shutdown, dependent on the OS.
//
// On Unix-like operating systems, it is important to catch SIGTERM in addition to SIGINT.
func GetSignals() []os.Signal {
return []os.Signal{os.Interrupt, syscall.SIGTERM}
}
+10
View File
@@ -0,0 +1,10 @@
//go:build !unix
package shutdown
import "os"
// GetSignals returns the appropriate signals to catch for a clean shutdown, dependent on the OS.
func GetSignals() []os.Signal {
return []os.Signal{os.Interrupt}
}