Created
August 24, 2017 16:42
-
-
Save jonbodner/c19af36b6f057b0ab007018af9298f01 to your computer and use it in GitHub Desktop.
future-blog-post-14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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