Package signal provides simple, semantic manipulation of the operating system's signal processing.
go get -u github.com/wjiec/go-signal
Listens to the user's signal to exit the program and performs cleanup
func main() {
f, _ := os.Open("path/to/your/config")
s, _ := http.NewServer(f)
signal.Once(syscall.SIGTERM).Notify(context.TODO(), func(sig os.Signal) {
_ = s.Shutdown()
_ = f.Close()
})
s.Start()
}
Listening for SIGUSR1
signals from users and performing services reload
func main() {
ngx, _ := nginx.New(cfg)
signal.When(syscall.SIGUSR1).Notify(context.TODO(), func(sig os.Signal) {
_ = ngx.Reload()
})
}
Create a context object using the specified signals and cancel the current context when the signal arrived
func main() {
var db *sql.DB
ctx, cancel := signal.With(context.TODO(), syscall.SIGTERM)
defer cancel()
_, _ = db.QueryContext(ctx, "select id,username,password from `user`")
}
Released under the MIT License.