//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
-func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
- if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
+func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
+ if cur, ok := parent.Deadline(); ok && cur.Before(d) {
// The current deadline is already sooner than the new one.
return WithCancel(parent)
}
c := &timerCtx{
cancelCtx: newCancelCtx(parent),
- deadline: deadline,
+ deadline: d,
}
propagateCancel(parent, c)
- d := time.Until(deadline)
- if d <= 0 {
+ dur := time.Until(d)
+ if dur <= 0 {
c.cancel(true, DeadlineExceeded) // deadline has already passed
return c, func() { c.cancel(true, Canceled) }
}
c.mu.Lock()
defer c.mu.Unlock()
if c.err == nil {
- c.timer = time.AfterFunc(d, func() {
+ c.timer = time.AfterFunc(dur, func() {
c.cancel(true, DeadlineExceeded)
})
}