README
¶
go-signal
Package signal provides simple, semantic manipulation of the operating system's signal processing.
Installation
go get -u github.com/wjiec/go-signal
Quick Start
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`")
}
License
Released under the MIT License.
Documentation
¶
Overview ¶
Package signal provides simple, semantic manipulation of the operating system's signal processing.
Index ¶
Constants ¶
const ( // SigCtx represents signal from cancelled context SigCtx = syscall.Signal(0xff) )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Notifier ¶
Notifier creates and listens to a specified system signal and calls a handler when the signal is received or when the context is cancelled.
Notifier will be closed when the context is cancelled and the handler can get an instance of SigCtx (number = 0xff).
methods of the Notifier can be safely called multiple times.