| OLD | NEW |
| 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 Loading... |
| 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 } |
| OLD | NEW |