diff --git a/lnutils/context.go b/lnutils/context.go new file mode 100644 index 000000000..5deeb7714 --- /dev/null +++ b/lnutils/context.go @@ -0,0 +1,22 @@ +package lnutils + +import "context" + +// ContextFromQuit returns a context that is cancelled when the provided quit +// channel is closed. The returned cancel function MUST be called to avoid +// goroutine leaks. +func ContextFromQuit(quit <-chan struct{}) (context.Context, + context.CancelFunc) { + + ctx, cancel := context.WithCancel(context.Background()) + + go func() { + select { + case <-quit: + cancel() + case <-ctx.Done(): + } + }() + + return ctx, cancel +}