Commit 298791a9 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

all: use time.Until where applicable

Updates #14595

Change-Id: Idf60b3004c7a0ebb59dd48389ab62c854069e09f
Reviewed-on: https://go-review.googlesource.com/28073
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 6c6ad08e
...@@ -376,7 +376,7 @@ func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) { ...@@ -376,7 +376,7 @@ func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
deadline: deadline, deadline: deadline,
} }
propagateCancel(parent, c) propagateCancel(parent, c)
d := deadline.Sub(time.Now()) d := time.Until(deadline)
if d <= 0 { if d <= 0 {
c.cancel(true, DeadlineExceeded) // deadline has already passed c.cancel(true, DeadlineExceeded) // deadline has already passed
return c, func() { c.cancel(true, Canceled) } return c, func() { c.cancel(true, Canceled) }
...@@ -406,7 +406,7 @@ func (c *timerCtx) Deadline() (deadline time.Time, ok bool) { ...@@ -406,7 +406,7 @@ func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
} }
func (c *timerCtx) String() string { func (c *timerCtx) String() string {
return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now())) return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, time.Until(c.deadline))
} }
func (c *timerCtx) cancel(removeFromParent bool, err error) { func (c *timerCtx) cancel(removeFromParent bool, err error) {
......
...@@ -102,7 +102,7 @@ func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (* ...@@ -102,7 +102,7 @@ func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*
timeout := dialer.Timeout timeout := dialer.Timeout
if !dialer.Deadline.IsZero() { if !dialer.Deadline.IsZero() {
deadlineTimeout := dialer.Deadline.Sub(time.Now()) deadlineTimeout := time.Until(dialer.Deadline)
if timeout == 0 || deadlineTimeout < timeout { if timeout == 0 || deadlineTimeout < timeout {
timeout = deadlineTimeout timeout = deadlineTimeout
} }
......
...@@ -122,7 +122,7 @@ func (fd *netFD) setWriteDeadline(t time.Time) error { ...@@ -122,7 +122,7 @@ func (fd *netFD) setWriteDeadline(t time.Time) error {
} }
func setDeadlineImpl(fd *netFD, t time.Time, mode int) error { func setDeadlineImpl(fd *netFD, t time.Time, mode int) error {
diff := int64(t.Sub(time.Now())) diff := int64(time.Until(t))
d := runtimeNano() + diff d := runtimeNano() + diff
if d <= 0 && diff > 0 { if d <= 0 && diff > 0 {
// If the user has a deadline in the future, but the delay calculation // If the user has a deadline in the future, but the delay calculation
......
...@@ -324,7 +324,7 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi ...@@ -324,7 +324,7 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi
var once sync.Once var once sync.Once
stopTimer = func() { once.Do(func() { close(stopTimerCh) }) } stopTimer = func() { once.Do(func() { close(stopTimerCh) }) }
timer := time.NewTimer(deadline.Sub(time.Now())) timer := time.NewTimer(time.Until(deadline))
go func() { go func() {
select { select {
case <-initialReqCancel: case <-initialReqCancel:
......
...@@ -1737,7 +1737,7 @@ restart: ...@@ -1737,7 +1737,7 @@ restart:
if !c.rd.IsZero() { if !c.rd.IsZero() {
// If the deadline falls in the middle of our sleep window, deduct // If the deadline falls in the middle of our sleep window, deduct
// part of the sleep, then return a timeout. // part of the sleep, then return a timeout.
if remaining := c.rd.Sub(time.Now()); remaining < cue { if remaining := time.Until(c.rd); remaining < cue {
c.script[0] = cue - remaining c.script[0] = cue - remaining
time.Sleep(remaining) time.Sleep(remaining)
return 0, syscall.ETIMEDOUT return 0, syscall.ETIMEDOUT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment