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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package clock 5 package clock
6 6
7 import ( 7 import (
8 "time" 8 "time"
9
10 "golang.org/x/net/context"
9 ) 11 )
10 12
11 // Implementation of Clock that uses Go's standard library. 13 // Implementation of Clock that uses Go's standard library.
12 type systemClock struct{} 14 type systemClock struct{}
13 15
14 // System clock instance. 16 // System clock instance.
15 var systemClockInstance systemClock 17 var systemClockInstance systemClock
16 18
17 var _ Clock = systemClock{} 19 var _ Clock = systemClock{}
18 20
19 // GetSystemClock returns an instance of a Clock whose method calls directly use 21 // GetSystemClock returns an instance of a Clock whose method calls directly use
20 // Go's "time" library. 22 // Go's "time" library.
21 func GetSystemClock() Clock { 23 func GetSystemClock() Clock {
22 return systemClockInstance 24 return systemClockInstance
23 } 25 }
24 26
25 func (systemClock) Now() time.Time { 27 func (systemClock) Now() time.Time {
26 return time.Now() 28 return time.Now()
27 } 29 }
28 30
29 func (systemClock) Sleep(d time.Duration) { 31 func (sc systemClock) Sleep(c context.Context, d time.Duration) error {
30 » time.Sleep(d) 32 » ar := <-sc.After(c, d)
33 » return ar.Err
31 } 34 }
32 35
33 func (systemClock) NewTimer() Timer { 36 func (systemClock) NewTimer(ctx context.Context) Timer {
34 » return new(systemTimer) 37 » t := systemTimer{
38 » » ctx: ctx,
39 » }
40 » t.reset()
41 » return &t
35 } 42 }
36 43
37 func (systemClock) After(d time.Duration) <-chan time.Time { 44 func (systemClock) After(c context.Context, d time.Duration) <-chan AfterResult {
38 » return time.After(d) 45 » ac := make(chan AfterResult)
46 » go func() {
dnj (Google) 2016/02/10 03:19:03 I implemented this separately from systemtimer b/c
47 » » ar := AfterResult{}
48
49 » » select {
50 » » case <-c.Done():
51 » » » ar.Err = c.Err()
52
53 » » case tm := <-time.After(d):
54 » » » // For determinism, prefer context cancellation over ful l sleep.
55 » » » select {
56 » » » case <-c.Done():
57 » » » » ar.Err = c.Err()
58 » » » default:
59 » » » » ar.Time = tm
60 » » » }
61 » » }
62 » » ac <- ar
63 » }()
64 » return ac
39 } 65 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698