-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxcontext.go
59 lines (46 loc) · 1.16 KB
/
xcontext.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
// Package xcontext implements indispensable context helpers.
package xcontext
import (
"context"
"time"
)
// WithoutCancel returns a context derived from ctx that may
// never be cancelled.
func WithoutCancel(ctx context.Context) context.Context {
return withoutCancel{ctx: ctx}
}
type withoutCancel struct {
ctx context.Context
}
func (c withoutCancel) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (c withoutCancel) Done() <-chan struct{} {
return nil
}
func (c withoutCancel) Err() error {
return nil
}
func (c withoutCancel) Value(key interface{}) interface{} {
return c.ctx.Value(key)
}
// WithoutValues creates a new context derived from ctx that does not inherit its values
// but does pass on cancellation.
func WithoutValues(ctx context.Context) context.Context {
return withoutValues{ctx: ctx}
}
type withoutValues struct {
ctx context.Context
}
func (c withoutValues) Deadline() (time.Time, bool) {
return c.ctx.Deadline()
}
func (c withoutValues) Done() <-chan struct{} {
return c.ctx.Done()
}
func (c withoutValues) Err() error {
return c.ctx.Err()
}
func (c withoutValues) Value(key interface{}) interface{} {
return nil
}