-
Notifications
You must be signed in to change notification settings - Fork 8
/
helpers.go
89 lines (69 loc) · 1.42 KB
/
helpers.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
package context
import (
"time"
"golang.org/x/net/context"
"github.com/Sirupsen/logrus"
)
func Background() Context {
return ctx{
Context: context.Background(),
Entry: std,
}
}
//func TODO() Context
func WithCancel(parent Context) (Context, context.CancelFunc) {
c, cancel := context.WithCancel(parent)
var entry *logrus.Entry
if ctx, ok := parent.(*ctx); ok {
entry = ctx.Entry
} else {
entry = std
}
ctx := ctx{
Context: c,
Entry: entry,
}
return &ctx, cancel
}
func WithDeadline(parent Context, deadline time.Time) (Context, context.CancelFunc) {
c, cancel := context.WithDeadline(parent, deadline)
var entry *logrus.Entry
if ctx, ok := parent.(*ctx); ok {
entry = ctx.Entry
} else {
entry = std
}
ctx := &ctx{
Context: c,
Entry: entry,
}
return ctx, cancel
}
func WithTimeout(parent Context, timeout time.Duration) (Context, context.CancelFunc) {
c, cancel := context.WithTimeout(parent, timeout)
var entry *logrus.Entry
if ctx, ok := parent.(*ctx); ok {
entry = ctx.Entry
} else {
entry = std
}
ctx := &ctx{
Context: c,
Entry: entry,
}
return ctx, cancel
}
func WithValue(parent Context, key string, value interface{}) Context {
c := context.WithValue(parent, key, value)
var entry *logrus.Entry
if ctx, ok := parent.(*ctx); ok {
entry = ctx.Entry
} else {
entry = std
}
ctx := &ctx{
Context: c,
Entry: entry.WithField(key, value),
}
return ctx
}