forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
44 lines (34 loc) · 845 Bytes
/
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
package imagor
import (
"context"
"time"
)
type contextKey struct {
name string
}
var detachedCtxKey = &contextKey{"Detached"}
type detached struct {
ctx context.Context
}
func (detached) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (detached) Done() <-chan struct{} {
return nil
}
func (detached) Err() error {
return nil
}
func (d detached) Value(key interface{}) interface{} {
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 context.WithValue(detached{ctx: ctx}, detachedCtxKey, true)
}
// IsDetached returns if context is detached
func IsDetached(ctx context.Context) bool {
_, ok := ctx.Value(detachedCtxKey).(bool)
return ok
}