You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
The text was updated successfully, but these errors were encountered:
I'm not convinced this is useful enough. Signals are a fairly heavy-weight mechanism, and usually cause some fairly heavy-weight action (or something small, like printing a message). I don't think many programs are going to simply want to cancel a context, which I think is neither a heavy-weight or a small action. And, as you say, it's easy enough to write yourself for programs that want it.
If the Deadline can be changed then each check in the flow of independent actions on the context can verify the deadline has not passed, effectively stopping the flow of action. I'm not sure what this looks like in practice, just got started with the context package as a solution. And doing a fancy time check is maybe not the most efficient approach.
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 howos/signal
works to be sure.Here's a protoype:
The text was updated successfully, but these errors were encountered: