-
Notifications
You must be signed in to change notification settings - Fork 89
/
context.go
110 lines (95 loc) · 2.5 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils
import (
"fmt"
"sync"
"time"
"golang.org/x/net/context"
"github.com/juju/clock"
)
// timerCtx is an implementation of context.Context that
// is done when a given deadline has passed
// (as measured by the Clock in the clock field)
type timerCtx struct {
clock clock.Clock
timer clock.Timer
deadline time.Time
parent context.Context
done chan struct{}
// mu guards err.
mu sync.Mutex
// err holds context.Canceled or context.DeadlineExceeded
// after the context has been canceled.
// If this is non-nil, then done will have been closed.
err error
}
func (ctx *timerCtx) Deadline() (time.Time, bool) {
return ctx.deadline, true
}
func (ctx *timerCtx) Err() error {
ctx.mu.Lock()
defer ctx.mu.Unlock()
return ctx.err
}
func (ctx *timerCtx) Value(key interface{}) interface{} {
return ctx.parent.Value(key)
}
func (ctx *timerCtx) Done() <-chan struct{} {
return ctx.done
}
func (ctx *timerCtx) cancel(err error) {
ctx.mu.Lock()
defer ctx.mu.Unlock()
if err == nil {
panic("cancel with nil error!")
}
if ctx.err != nil {
// Already canceled - no need to do anything.
return
}
ctx.err = err
if ctx.timer != nil {
ctx.timer.Stop()
}
close(ctx.done)
}
func (ctx *timerCtx) String() string {
return fmt.Sprintf("%v.WithDeadline(%s [%s])", ctx.parent, ctx.deadline, ctx.deadline.Sub(ctx.clock.Now()))
}
// ContextWithTimeout is like context.WithTimeout
// except that it works with a clock.Clock rather than
// wall-clock time.
func ContextWithTimeout(parent context.Context, clk clock.Clock, timeout time.Duration) (context.Context, context.CancelFunc) {
return ContextWithDeadline(parent, clk, clk.Now().Add(timeout))
}
// ContextWithDeadline is like context.WithDeadline
// except that it works with a clock.Clock rather than
// wall-clock time.
func ContextWithDeadline(parent context.Context, clk clock.Clock, deadline time.Time) (context.Context, context.CancelFunc) {
d := deadline.Sub(clk.Now())
ctx := &timerCtx{
clock: clk,
parent: parent,
deadline: deadline,
done: make(chan struct{}),
}
if d <= 0 {
// deadline has already passed
ctx.cancel(context.DeadlineExceeded)
return ctx, func() {}
}
ctx.timer = clk.NewTimer(d)
go func() {
select {
case <-ctx.timer.Chan():
ctx.cancel(context.DeadlineExceeded)
case <-parent.Done():
ctx.cancel(parent.Err())
case <-ctx.done:
}
}()
return ctx, func() {
ctx.cancel(context.Canceled)
}
}