Skip to content

🚦provides simple, semantic manipulation of the operating system's signal processing.

License

Notifications You must be signed in to change notification settings

wjiec/go-signal

Repository files navigation

go-signal

Go Reference Go Report Card

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.

About

🚦provides simple, semantic manipulation of the operating system's signal processing.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages