-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
48 lines (37 loc) · 823 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
45
46
47
48
//go:build !go1.21
package memoize
import (
"context"
"reflect"
"time"
)
var _ context.Context = (*detachedContext)(nil)
func withoutCancel(ctx context.Context) context.Context {
return &detachedContext{ctx}
}
// detachedContext is never canceled and no deadline.
// whether it has values.
type detachedContext struct {
context.Context
}
func (*detachedContext) Deadline() (deadline time.Time, ok bool) {
return
}
func (*detachedContext) Done() <-chan struct{} {
return nil
}
func (*detachedContext) Err() error {
return nil
}
type stringer interface {
String() string
}
func contextName(c context.Context) string {
if s, ok := c.(stringer); ok {
return s.String()
}
return reflect.TypeOf(c).String()
}
func (ctx *detachedContext) String() string {
return contextName(ctx.Context) + ".Detached"
}