-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
97 lines (78 loc) · 2.26 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
package glock
import (
"context"
"sync"
"time"
)
type ctxKey struct{}
var glockCtxKey = &ctxKey{}
// WithContext returns a context derived from the provided context with
// the provided Clock as a value.
func WithContext(ctx context.Context, clock Clock) context.Context {
return context.WithValue(ctx, glockCtxKey, clock)
}
// FromContext retrieves the Clock value from the provided context. If a Clock
// is not set on the context a new real clock will be returned.
func FromContext(ctx context.Context) Clock {
clock, ok := ctx.Value(glockCtxKey).(Clock)
if !ok {
clock = NewRealClock()
}
return clock
}
type glockAwareContext struct {
context.Context
mu sync.Mutex
err error
done chan struct{}
}
// ContextWithDeadline mimmics context.WithDeadline, but uses the given clock instance
// instead of the using standard time.After function directly.
func ContextWithDeadline(ctx context.Context, clock Clock, deadline time.Time) (context.Context, context.CancelFunc) {
return ContextWithTimeout(ctx, clock, deadline.Sub(clock.Now()))
}
// ContextWithTimeout mimmics context.WithTimeout, but uses the given clock instance
// instead of the using standard time.After function directly.
func ContextWithTimeout(ctx context.Context, clock Clock, timeout time.Duration) (context.Context, context.CancelFunc) {
done := make(chan struct{})
canceled := make(chan struct{})
ctx, cancel := context.WithCancel(ctx)
child := &glockAwareContext{Context: ctx, done: done}
go func() {
defer cancel()
defer close(done)
child.setErr(watchContext(ctx, canceled, clock.After(timeout)))
}()
return child, closeOnce(canceled)
}
func (ctx *glockAwareContext) Done() <-chan struct{} {
return ctx.done
}
func (ctx *glockAwareContext) Err() error {
ctx.mu.Lock()
defer ctx.mu.Unlock()
return ctx.err
}
func (ctx *glockAwareContext) setErr(err error) {
ctx.mu.Lock()
defer ctx.mu.Unlock()
ctx.err = err
}
func watchContext(ctx context.Context, canceled <-chan struct{}, afterCh <-chan time.Time) error {
select {
case <-canceled:
return context.Canceled
case <-afterCh:
return context.DeadlineExceeded
case <-ctx.Done():
return ctx.Err()
}
}
func closeOnce(ch chan<- struct{}) context.CancelFunc {
var once sync.Once
return func() {
once.Do(func() {
close(ch)
})
}
}