OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package clock | |
6 | |
7 import ( | |
8 "time" | |
9 | |
10 "golang.org/x/net/context" | |
11 ) | |
12 | |
13 // CancelSleep sleeps the current goroutine (see time.Sleep). | |
14 // | |
15 // If the supplied Context is canceled prior to the specified duration, | |
16 // CancelSleep will return the Context's error. If the sleep completes | |
17 // naturally, it will return nil. | |
18 func CancelSleep(c context.Context, d time.Duration) error { | |
iannucci
2016/02/09 20:41:10
(discussed offline) let's just make normal Sleep (
Vadim Sh.
2016/02/09 21:11:37
I'd prefer to keep CancelSleep. clock package is m
| |
19 select { | |
20 case <-c.Done(): | |
21 return c.Err() | |
22 | |
23 case <-After(c, d): | |
24 // For determinism, prefer context cancellation over full sleep. | |
25 select { | |
26 case <-c.Done(): | |
27 return c.Err() | |
28 default: | |
29 break | |
30 } | |
31 return nil | |
32 } | |
33 } | |
OLD | NEW |