From cb9c80758ce4ef49c450ec2e289d200422c30018 Mon Sep 17 00:00:00 2001 From: Sameer Ajmani Date: Mon, 28 Jul 2014 21:01:30 -0400 Subject: go.blog/context: an article about go.net/context.Context. Blog demo (internal): http://olivia.nyc.corp.google.com:8081/context Server demo (internal): http://olivia.nyc.corp.google.com:8080/search?q=golang&timeout=1s LGTM=bcmills, adg, r R=r, rsc, adg, bcmills, ken, adonovan, dsymonds, crawshaw, campoy, hakim, dneil https://golang.org/cl/116820044 --- content/context/tomb/tomb.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 content/context/tomb/tomb.go (limited to 'content/context/tomb') 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 +} -- cgit v1.2.3