Skip to content

Commit 298791a

Browse files
committed
all: use time.Until where applicable
Updates #14595 Change-Id: Idf60b3004c7a0ebb59dd48389ab62c854069e09f Reviewed-on: https://go-review.googlesource.com/28073 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Andrew Gerrand <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 6c6ad08 commit 298791a

File tree

5 files changed

+6
-6
lines changed

5 files changed

+6
-6
lines changed

src/context/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
376376
deadline: deadline,
377377
}
378378
propagateCancel(parent, c)
379-
d := deadline.Sub(time.Now())
379+
d := time.Until(deadline)
380380
if d <= 0 {
381381
c.cancel(true, DeadlineExceeded) // deadline has already passed
382382
return c, func() { c.cancel(true, Canceled) }
@@ -406,7 +406,7 @@ func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
406406
}
407407

408408
func (c *timerCtx) String() string {
409-
return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))
409+
return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, time.Until(c.deadline))
410410
}
411411

412412
func (c *timerCtx) cancel(removeFromParent bool, err error) {

src/crypto/tls/tls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*
102102
timeout := dialer.Timeout
103103

104104
if !dialer.Deadline.IsZero() {
105-
deadlineTimeout := dialer.Deadline.Sub(time.Now())
105+
deadlineTimeout := time.Until(dialer.Deadline)
106106
if timeout == 0 || deadlineTimeout < timeout {
107107
timeout = deadlineTimeout
108108
}

src/net/fd_poll_runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (fd *netFD) setWriteDeadline(t time.Time) error {
122122
}
123123

124124
func setDeadlineImpl(fd *netFD, t time.Time, mode int) error {
125-
diff := int64(t.Sub(time.Now()))
125+
diff := int64(time.Until(t))
126126
d := runtimeNano() + diff
127127
if d <= 0 && diff > 0 {
128128
// If the user has a deadline in the future, but the delay calculation

src/net/http/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi
324324
var once sync.Once
325325
stopTimer = func() { once.Do(func() { close(stopTimerCh) }) }
326326

327-
timer := time.NewTimer(deadline.Sub(time.Now()))
327+
timer := time.NewTimer(time.Until(deadline))
328328
go func() {
329329
select {
330330
case <-initialReqCancel:

src/net/http/serve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ restart:
17371737
if !c.rd.IsZero() {
17381738
// If the deadline falls in the middle of our sleep window, deduct
17391739
// part of the sleep, then return a timeout.
1740-
if remaining := c.rd.Sub(time.Now()); remaining < cue {
1740+
if remaining := time.Until(c.rd); remaining < cue {
17411741
c.script[0] = cue - remaining
17421742
time.Sleep(remaining)
17431743
return 0, syscall.ETIMEDOUT

0 commit comments

Comments
 (0)