Skip to content

About

Provides an easy way of handling exit signals in go by providing a context

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

go-exitcontext

test

A Go package that returns a context.Context which is cancelled when the process receives SIGINT or SIGTERM, to shut programs down gracefully.

Installation

go get github.com/aklinkert/go-exitcontext

Requires Go 1.21 or newer.

Usage

If you already have a context and want to recycle that one:

ctx := context.Background()
exitCtx := exitcontext.NewWithContext(ctx)

else just use New, which creates a context.Background() under the hood:

exitCtx := exitcontext.New()

<-exitCtx.Done() // SIGINT or SIGTERM received

The signal handler is released as soon as the context is done, whether a signal arrived or the parent context was cancelled. A second SIGINT (Ctrl-C) or SIGTERM after that gets Go's default behaviour and terminates the process, same as with signal.NotifyContext.

For new code, the standard library's signal.NotifyContext offers the same with an explicit stop function.

License

MIT, see LICENSE.

About

Provides an easy way of handling exit signals in go by providing a context

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages