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

Side by Side Diff: common/clock/clockcontext.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: More test coverage, cleanup, consolidation. 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "sync" 8 "sync"
9 "time" 9 "time"
10 10
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 d := deadline.Sub(Now(c)) 64 d := deadline.Sub(Now(c))
65 if d <= 0 { 65 if d <= 0 {
66 // Deadline has already passed. 66 // Deadline has already passed.
67 c.setError(context.DeadlineExceeded) 67 c.setError(context.DeadlineExceeded)
68 cancelFunc() 68 cancelFunc()
69 return c, cancelFunc 69 return c, cancelFunc
70 } 70 }
71 71
72 // Invoke our cancelFunc after the specified time. 72 // Invoke our cancelFunc after the specified time.
73 go func() { 73 go func() {
74 » » select { 74 » » if ar := <-After(c, d); ar.Err == nil {
75 » » case <-c.Done(): 75 » » » // Timer expired naturally.
76 » » » break
77
78 » » case <-After(c, d):
79 c.setError(context.DeadlineExceeded) 76 c.setError(context.DeadlineExceeded)
80 cancelFunc() 77 cancelFunc()
81 } 78 }
82 }() 79 }()
83 return c, cancelFunc 80 return c, cancelFunc
84 } 81 }
85 82
86 // WithTimeout is a clock library implementation of context.WithTimeout that 83 // WithTimeout is a clock library implementation of context.WithTimeout that
87 // uses the clock library's time features instead of the Go time library. 84 // uses the clock library's time features instead of the Go time library.
88 // 85 //
89 // For more information, see context.WithTimeout. 86 // For more information, see context.WithTimeout.
90 func WithTimeout(parent context.Context, timeout time.Duration) (context.Context , context.CancelFunc) { 87 func WithTimeout(parent context.Context, timeout time.Duration) (context.Context , context.CancelFunc) {
91 return WithDeadline(parent, Now(parent).Add(timeout)) 88 return WithDeadline(parent, Now(parent).Add(timeout))
92 } 89 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698