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

Unified Diff: common/clock/timer.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Actually upload the patch. 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
« no previous file with comments | « common/clock/testclock/testtimer_test.go ('k') | common/gcloud/pubsub/ackbuffer/ackbuffer.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/clock/timer.go
diff --git a/common/clock/timer.go b/common/clock/timer.go
index aa03c383dda4f710b8c1745d62fca5d645a9f1c8..6b0a47b5443efa84a80b9511ca8869c04bdd8841 100644
--- a/common/clock/timer.go
+++ b/common/clock/timer.go
@@ -13,19 +13,39 @@ import (
// A Timer is instantiated from a Clock instance and started via its Reset()
// method.
type Timer interface {
- // GetC returns the underlying timer's channel, or nil if the timer is no
- // running.
- GetC() <-chan time.Time
+ // GetC returns the underlying timer's channel.
+ //
+ // If the Timer is interrupted via Stop, its channel will block indefinitely.
+ GetC() <-chan TimerResult
// Reset configures the timer to expire after a specified duration.
//
// If the timer is already running, its previous state will be cleared and
- // this method will return true.
+ // this method will return true. The channel returned by GetC() will not
+ // change due to Reset.
Reset(d time.Duration) bool
// Stop clears any timer tasking, rendering it inactive.
//
// Stop may be called on an inactive timer, in which case nothing will happen
- // If the timer is active, it will be stopped and this method will return true.
+ // If the timer is active, it will be stopped and this method will return
+ // true.
+ //
+ // If a timer is stopped, its GetC channel will block indefinitely to avoid
+ // erroneously unblocking goroutines that are waiting on it. This is
+ // consistent with time.Timer.
Stop() bool
}
+
+// TimerResult is the result for a timer operation.
+//
+// Time will be set to the time when the result was generated. If the source of
+// the result was prematurely terminated (e.g., due to Context cancellation)
+// Err will be non-nil.
+type TimerResult struct {
+ time.Time
+
+ // Err, if not nil, indicates that After did not finish naturally and contains
+ // the reason why.
+ Err error
+}
« no previous file with comments | « common/clock/testclock/testtimer_test.go ('k') | common/gcloud/pubsub/ackbuffer/ackbuffer.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698