This repository provides helpers with POSIX signal(7)
go get -u github.com/gol4ng/signal
Signal subscriber that allows you to attach a callback to an os.Signal
notification.
Useful to react to any os.Signal.
It returns an unsubscribe
function that can gracefully stop some http server and clean allocated object
⚠️ If you defer unsubcribe dont forget the final()
, If you forget it go defer will execute the subscribe process
defer signal.Subscribe(func(){}, signals...)()
package main
import (
"fmt"
"os"
"syscall"
"time"
"github.com/gol4ng/signal"
)
func main() {
defer signal.Subscribe(func(signal os.Signal) {
fmt.Println("this code is execute when signal os.Interrupt, syscall.SIGTERM was received")
}, os.Interrupt, syscall.SIGTERM)()
// your application code here
}
The killer subscriber will register your signal handler,
but it register another one that gonna to kill (os.Exit
) application if 2 killer signals was received.
package main
import (
"fmt"
"os"
"time"
"syscall"
"github.com/gol4ng/signal"
)
func main() {
defer signal.SubscribeWithKiller(func(signal os.Signal) {
// here you can implement your application stopping steps
fmt.Println("this code is execute when signal os.Interrupt, syscall.SIGTERM was received")
}, os.Interrupt, syscall.SIGTERM)()
// your application code here
}