forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
98 lines (81 loc) · 1.89 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
package imagor
import (
"context"
"errors"
"sync"
"time"
)
type contextKey struct {
Type int8
}
var imagorContextKey = contextKey{1}
var detachContextKey = contextKey{2}
type imagorContextRef struct {
funcs []func()
l sync.Mutex
Blob *Blob
}
func (r *imagorContextRef) Defer(fn func()) {
r.l.Lock()
r.funcs = append(r.funcs, fn)
r.l.Unlock()
}
func (r *imagorContextRef) Done() {
r.l.Lock()
for _, fn := range r.funcs {
fn()
}
r.funcs = nil
r.l.Unlock()
}
// withContext context with imagor defer handling and cache
func withContext(ctx context.Context) context.Context {
if r, ok := ctx.Value(imagorContextKey).(*imagorContextRef); ok && r != nil {
return ctx
}
r := &imagorContextRef{}
ctx = context.WithValue(ctx, imagorContextKey, r)
go func() {
<-ctx.Done()
r.Done()
}()
return ctx
}
func mustContextRef(ctx context.Context) *imagorContextRef {
if r, ok := ctx.Value(imagorContextKey).(*imagorContextRef); ok && r != nil {
return r
}
panic(errors.New("not imagor context"))
}
// contextDefer add func to context, defer called at the end of request
func contextDefer(ctx context.Context, fn func()) {
mustContextRef(ctx).Defer(fn)
}
type detachedContext struct {
ctx context.Context
}
func (detachedContext) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (detachedContext) Done() <-chan struct{} {
return nil
}
func (detachedContext) Err() error {
return nil
}
func (d detachedContext) Value(key any) any {
if key == detachContextKey {
return true
}
return d.ctx.Value(key)
}
// detachContext returns a context that keeps all the values of its parent context
// but detaches from cancellation and timeout
func detachContext(ctx context.Context) context.Context {
return detachedContext{ctx: ctx}
}
// isDetached returns if context is detached
func isDetached(ctx context.Context) bool {
_, ok := ctx.Value(detachContextKey).(bool)
return ok
}