mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
tstime: add Sleep (#7480)
Sleep is an interruptible sleep variation. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
parent
3b18e65c6a
commit
9112e78925
@ -5,6 +5,7 @@
|
|||||||
package tstime
|
package tstime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -164,3 +165,16 @@ func ParseDuration(s string) (time.Duration, error) {
|
|||||||
}
|
}
|
||||||
return time.ParseDuration(s)
|
return time.ParseDuration(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sleep is like [time.Sleep] but returns early upon context cancelation.
|
||||||
|
// It reports whether the full sleep duration was achieved.
|
||||||
|
func Sleep(ctx context.Context, d time.Duration) bool {
|
||||||
|
timer := time.NewTimer(d)
|
||||||
|
defer timer.Stop()
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return false
|
||||||
|
case <-timer.C:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user