aboutsummaryrefslogtreecommitdiff
path: root/content/context/tomb/tomb.go
diff options
context:
space:
mode:
Diffstat (limited to 'content/context/tomb/tomb.go')
1 files changed, 22 insertions, 0 deletions
diff --git a/content/context/tomb/tomb.go b/content/context/tomb/tomb.go
new file mode 100644
index 0000000..8a2142f
--- /dev/null
+++ b/content/context/tomb/tomb.go
@@ -0,0 +1,22 @@
+// Package tomb provides a Context implementation that is canceled when either
+// its parent Context is canceled or a provided Tomb is killed.
+package tomb
+
+import (
+ "code.google.com/p/go.net/context"
+ tomb "gopkg.in/tomb.v2"
+)
+
+// NewContext returns a Context that is canceled either when parent is canceled
+// or when t is Killed.
+func NewContext(parent context.Context, t *tomb.Tomb) context.Context {
+ ctx, cancel := context.WithCancel(parent)
+ go func() {
+ select {
+ case <-t.Dying():
+ cancel()
+ case <-ctx.Done():
+ }
+ }()
+ return ctx
+}