Skip to content

Instantly share code, notes, and snippets.

@MichaelSnowden
Created April 25, 2023 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelSnowden/542117f05ede85ca53303963dad05e68 to your computer and use it in GitHub Desktop.
Save MichaelSnowden/542117f05ede85ca53303963dad05e68 to your computer and use it in GitHub Desktop.
A method of propagating cancellation signals to a context
package signalprop
import (
"context"
)
func PropagateCancel(ctx context.Context, done chan struct{}) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx)
go func() {
select {
case <-ctx.Done():
case <-done:
cancel()
}
}()
return ctx, cancel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment