1
2
3
4
5
6
7 package context
8
9 import (
10 "errors"
11 "fmt"
12 "sync"
13 "time"
14 )
15
16
17
18 type emptyCtx int
19
20 func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
21 return
22 }
23
24 func (*emptyCtx) Done() <-chan struct{} {
25 return nil
26 }
27
28 func (*emptyCtx) Err() error {
29 return nil
30 }
31
32 func (*emptyCtx) Value(key interface{}) interface{} {
33 return nil
34 }
35
36 func (e *emptyCtx) String() string {
37 switch e {
38 case background:
39 return "context.Background"
40 case todo:
41 return "context.TODO"
42 }
43 return "unknown empty Context"
44 }
45
46 var (
47 background = new(emptyCtx)
48 todo = new(emptyCtx)
49 )
50
51
52 var Canceled = errors.New("context canceled")
53
54
55
56 var DeadlineExceeded = errors.New("context deadline exceeded")
57
58
59
60
61
62
63
64 func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
65 c := newCancelCtx(parent)
66 propagateCancel(parent, c)
67 return c, func() { c.cancel(true, Canceled) }
68 }
69
70
71 func newCancelCtx(parent Context) *cancelCtx {
72 return &cancelCtx{
73 Context: parent,
74 done: make(chan struct{}),
75 }
76 }
77
78
79 func propagateCancel(parent Context, child canceler) {
80 if parent.Done() == nil {
81 return
82 }
83 if p, ok := parentCancelCtx(parent); ok {
84 p.mu.Lock()
85 if p.err != nil {
86
87 child.cancel(false, p.err)
88 } else {
89 if p.children == nil {
90 p.children = make(map[canceler]bool)
91 }
92 p.children[child] = true
93 }
94 p.mu.Unlock()
95 } else {
96 go func() {
97 select {
98 case <-parent.Done():
99 child.cancel(false, parent.Err())
100 case <-child.Done():
101 }
102 }()
103 }
104 }
105
106
107
108
109 func parentCancelCtx(parent Context) (*cancelCtx, bool) {
110 for {
111 switch c := parent.(type) {
112 case *cancelCtx:
113 return c, true
114 case *timerCtx:
115 return c.cancelCtx, true
116 case *valueCtx:
117 parent = c.Context
118 default:
119 return nil, false
120 }
121 }
122 }
123
124
125 func removeChild(parent Context, child canceler) {
126 p, ok := parentCancelCtx(parent)
127 if !ok {
128 return
129 }
130 p.mu.Lock()
131 if p.children != nil {
132 delete(p.children, child)
133 }
134 p.mu.Unlock()
135 }
136
137
138
139 type canceler interface {
140 cancel(removeFromParent bool, err error)
141 Done() <-chan struct{}
142 }
143
144
145
146 type cancelCtx struct {
147 Context
148
149 done chan struct{}
150
151 mu sync.Mutex
152 children map[canceler]bool
153 err error
154 }
155
156 func (c *cancelCtx) Done() <-chan struct{} {
157 return c.done
158 }
159
160 func (c *cancelCtx) Err() error {
161 c.mu.Lock()
162 defer c.mu.Unlock()
163 return c.err
164 }
165
166 func (c *cancelCtx) String() string {
167 return fmt.Sprintf("%v.WithCancel", c.Context)
168 }
169
170
171
172 func (c *cancelCtx) cancel(removeFromParent bool, err error) {
173 if err == nil {
174 panic("context: internal error: missing cancel error")
175 }
176 c.mu.Lock()
177 if c.err != nil {
178 c.mu.Unlock()
179 return
180 }
181 c.err = err
182 close(c.done)
183 for child := range c.children {
184
185 child.cancel(false, err)
186 }
187 c.children = nil
188 c.mu.Unlock()
189
190 if removeFromParent {
191 removeChild(c.Context, c)
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204 func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
205 if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
206
207 return WithCancel(parent)
208 }
209 c := &timerCtx{
210 cancelCtx: newCancelCtx(parent),
211 deadline: deadline,
212 }
213 propagateCancel(parent, c)
214 d := deadline.Sub(time.Now())
215 if d <= 0 {
216 c.cancel(true, DeadlineExceeded)
217 return c, func() { c.cancel(true, Canceled) }
218 }
219 c.mu.Lock()
220 defer c.mu.Unlock()
221 if c.err == nil {
222 c.timer = time.AfterFunc(d, func() {
223 c.cancel(true, DeadlineExceeded)
224 })
225 }
226 return c, func() { c.cancel(true, Canceled) }
227 }
228
229
230
231
232 type timerCtx struct {
233 *cancelCtx
234 timer *time.Timer
235
236 deadline time.Time
237 }
238
239 func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
240 return c.deadline, true
241 }
242
243 func (c *timerCtx) String() string {
244 return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))
245 }
246
247 func (c *timerCtx) cancel(removeFromParent bool, err error) {
248 c.cancelCtx.cancel(false, err)
249 if removeFromParent {
250
251 removeChild(c.cancelCtx.Context, c)
252 }
253 c.mu.Lock()
254 if c.timer != nil {
255 c.timer.Stop()
256 c.timer = nil
257 }
258 c.mu.Unlock()
259 }
260
261
262
263
264
265
266
267
268
269
270
271 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
272 return WithDeadline(parent, time.Now().Add(timeout))
273 }
274
275
276
277
278
279
280 func WithValue(parent Context, key interface{}, val interface{}) Context {
281 return &valueCtx{parent, key, val}
282 }
283
284
285
286 type valueCtx struct {
287 Context
288 key, val interface{}
289 }
290
291 func (c *valueCtx) String() string {
292 return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
293 }
294
295 func (c *valueCtx) Value(key interface{}) interface{} {
296 if c.key == key {
297 return c.val
298 }
299 return c.Context.Value(key)
300 }
301
View as plain text