Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created August 24, 2017 16:42
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 jonbodner/c19af36b6f057b0ab007018af9298f01 to your computer and use it in GitHub Desktop.
Save jonbodner/c19af36b6f057b0ab007018af9298f01 to your computer and use it in GitHub Desktop.
future-blog-post-14
type futureImpl struct {
done chan struct{}
cancel chan struct{}
val interface{}
err error
o *sync.Once
}
func (f *futureImpl) Cancel() {
select {
case <-f.done:
//already finished
case <-f.cancel:
//already cancelled
default:
f.o.Do(func() {
close(f.cancel) //should only be called once, since the closed cancel channel will always return
})
}
}
func (f *futureImpl) IsCancelled() bool {
select {
case <-f.cancel:
return true
default:
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment