-
Notifications
You must be signed in to change notification settings - Fork 194
/
context.go
160 lines (130 loc) · 3.47 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2016 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to writing, software distributed
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
package contextimpl
import (
"errors"
"reflect"
"sync"
"time"
)
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
type emptyCtx int
func (emptyCtx) Deadline() (deadline time.Time, ok bool) { return }
func (emptyCtx) Done() <-chan struct{} { return nil }
func (emptyCtx) Err() error { return nil }
func (emptyCtx) Value(key interface{}) interface{} { return nil }
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
func Background() Context { return background }
func TODO() Context { return todo }
type cancelCtx struct {
Context
done chan struct{}
err error
mu sync.Mutex
}
func (ctx *cancelCtx) Done() <-chan struct{} { return ctx.done }
func (ctx *cancelCtx) Err() error {
ctx.mu.Lock()
defer ctx.mu.Unlock()
return ctx.err
}
var Canceled = errors.New("context canceled")
type CancelFunc func()
func WithCancel(parent Context) (Context, CancelFunc) {
ctx := &cancelCtx{
Context: parent,
done: make(chan struct{}),
}
cancel := func() { ctx.cancel(Canceled) }
go func() {
select {
case <-parent.Done():
ctx.cancel(parent.Err())
case <-ctx.Done():
}
}()
return ctx, cancel
}
func (ctx *cancelCtx) cancel(err error) {
ctx.mu.Lock()
defer ctx.mu.Unlock()
if ctx.err != nil {
return
}
ctx.err = err
close(ctx.done)
}
type deadlineCtx struct {
*cancelCtx
deadline time.Time
}
func (ctx *deadlineCtx) Deadline() (deadline time.Time, ok bool) {
return ctx.deadline, true
}
var DeadlineExceeded = deadlineExceededErr{}
type deadlineExceededErr struct{}
func (deadlineExceededErr) Error() string { return "deadline exceeded " }
func (deadlineExceededErr) Timeout() bool { return true }
func (deadlineExceededErr) Temporary() bool { return true }
func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
cctx, cancel := WithCancel(parent)
ctx := &deadlineCtx{
cancelCtx: cctx.(*cancelCtx),
deadline: deadline,
}
// the line below has been replaced to work with version previous to go 1.8.
// timeout := time.Until(deadline)
timeout := deadline.Sub(time.Now())
t := time.AfterFunc(timeout, func() {
ctx.cancel(DeadlineExceeded)
})
stop := func() {
t.Stop()
cancel()
}
return ctx, stop
}
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
type valueCtx struct {
Context
value, key interface{}
}
func (ctx *valueCtx) Value(key interface{}) interface{} {
if key == ctx.key {
return ctx.value
}
return ctx.Context.Value(key)
}
func WithValue(parent Context, key, value interface{}) Context {
if key == nil {
panic("key is nil")
}
if !reflect.TypeOf(key).Comparable() {
panic("key is not comparable")
}
return &valueCtx{
Context: parent,
key: key,
value: value,
}
}