Closed
Description
Add a new function to os/signal
which creates a context which is canceled when a matched signal is received. It's very simple. In fact, it's so simple that I won't be surprised if this gets rejected purely on the basis of it being easy to just write when you need it, but it's also possible that better mechanisms can be implemented by putting it in the actual package and giving it access to the package's innards. I'm not familiar enough with how os/signal
works to be sure.
Here's a protoype:
func WithContext(ctx context.Context, sig ...os.Signal) context.Context {
ctx, cancel := context.WithCancel(ctx)
go func() {
c := make(chan os.Signal)
signal.Notify(c, sig...)
defer signal.Stop(c)
select {
case <-ctx.Done():
case <-c:
cancel()
}
}()
return ctx
}