Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Unified Diff: common/clock/systemclock.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Much more invasive, cancel by default, remove meter package. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: common/clock/systemclock.go
diff --git a/common/clock/systemclock.go b/common/clock/systemclock.go
index b5384a411fed547f99b9067c52abd09f2d2b38b6..41a96f3220fcaddda9e254c4d344cd0ae19efa65 100644
--- a/common/clock/systemclock.go
+++ b/common/clock/systemclock.go
@@ -6,6 +6,8 @@ package clock
import (
"time"
+
+ "golang.org/x/net/context"
)
// Implementation of Clock that uses Go's standard library.
@@ -26,14 +28,38 @@ func (systemClock) Now() time.Time {
return time.Now()
}
-func (systemClock) Sleep(d time.Duration) {
- time.Sleep(d)
+func (sc systemClock) Sleep(c context.Context, d time.Duration) error {
+ ar := <-sc.After(c, d)
+ return ar.Err
}
-func (systemClock) NewTimer() Timer {
- return new(systemTimer)
+func (systemClock) NewTimer(ctx context.Context) Timer {
+ t := systemTimer{
+ ctx: ctx,
+ }
+ t.reset()
+ return &t
}
-func (systemClock) After(d time.Duration) <-chan time.Time {
- return time.After(d)
+func (systemClock) After(c context.Context, d time.Duration) <-chan AfterResult {
+ ac := make(chan AfterResult)
+ go func() {
dnj (Google) 2016/02/10 03:19:03 I implemented this separately from systemtimer b/c
+ ar := AfterResult{}
+
+ select {
+ case <-c.Done():
+ ar.Err = c.Err()
+
+ case tm := <-time.After(d):
+ // For determinism, prefer context cancellation over full sleep.
+ select {
+ case <-c.Done():
+ ar.Err = c.Err()
+ default:
+ ar.Time = tm
+ }
+ }
+ ac <- ar
+ }()
+ return ac
}

Powered by Google App Engine
This is Rietveld 408576698