/
context_keys.go
242 lines (181 loc) · 5.02 KB
/
context_keys.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package context
import (
"sync"
"github.com/akutz/goof"
"github.com/codedellemc/rexray/libstorage/api/types"
)
// Key is the type used as a context key.
type Key int
const (
_ Key = -1 - iota
// PathConfigKey is a context key.
PathConfigKey
// LoggerKey is a context key.
LoggerKey
// HTTPRequestKey is a context key.
HTTPRequestKey
// AllInstanceIDsKey is the key for the map[string]*types.InstanceID value
// that maps all drivers to their instance IDs.
AllInstanceIDsKey
// LocalDevicesKey is a context key.
LocalDevicesKey
// AllLocalDevicesKey is the key for the map[string]*types.LocalDevices
// value that maps all drivers to their instance IDs.
AllLocalDevicesKey
// AdminTokenKey is the key for the server's admin token.
AdminTokenKey
// SessionKey is the key for the storage driver's session.
SessionKey
// EncodedAuthTokenKey is the key for an encoded authentication token.
EncodedAuthTokenKey
// keyLoggable is the minimum value from which the succeeding keys should
// be checked when logging.
keyLoggable
// AuthTokenKey is a context key.
AuthTokenKey
// ClientKey is a context key.
ClientKey
// TaskKey is a context key.
TaskKey
// InstanceIDKey is a context key.
InstanceIDKey
// ProfileKey is a context key.
ProfileKey
// RouteKey is a context key.
RouteKey
// ServerKey is a context key.
ServerKey
// ServiceKey is an alias for StorageService.
ServiceKey
// StorageServiceKey is a context key.
StorageServiceKey
// TransactionKey is a context key.
TransactionKey
// DriverKey is an alias for StorageDriver.
DriverKey
// UserKey is a context key.
UserKey
// HostKey is a context key.
HostKey
// TLSKey is a context key.
TLSKey
// keyEOF should always be the final key
keyEOF
)
// String returns the name of the context key.
func (k Key) String() string {
if v, ok := keyNames[k]; ok {
return v
}
return ""
}
var (
keyNames = map[Key]string{
AuthTokenKey: "token",
TaskKey: "task",
InstanceIDKey: "instanceID",
ProfileKey: "profile",
RouteKey: "route",
ServerKey: "server",
ServiceKey: "service",
StorageServiceKey: "service",
TransactionKey: "tx",
DriverKey: "storageDriver",
UserKey: "user",
HostKey: "host",
TLSKey: "tls",
}
)
// CustomKeyTypes is a bitmask used when registering a custom key with the
// context at runtime.
type CustomKeyTypes int
const (
// CustomLoggerKey indicates a value set for this key should be logged as
// a member of a log entry's fields collection when using the context's
// structured logger.
CustomLoggerKey CustomKeyTypes = 1 << iota
// CustomHeaderKey indicates a value set for this key should be sent along
// with HTTP requests as an HTTP header.
CustomHeaderKey
)
type customKey struct {
internalID int
externalID interface{}
keyBitmask CustomKeyTypes
}
var (
customKeys = map[interface{}]*customKey{}
customKeysRWL = &sync.RWMutex{}
)
func isCustomKey(key interface{}) (int, bool) {
return isCustomKeyWithLockOpts(key, true)
}
func isCustomKeyWithLockOpts(key interface{}, lock bool) (int, bool) {
if lock {
customKeysRWL.RLock()
defer customKeysRWL.RUnlock()
}
if v, ok := customKeys[key]; ok {
return v.internalID, true
}
return 0, false
}
// RegisterCustomKey registers a custom key with the context package.
func RegisterCustomKey(key interface{}, mask CustomKeyTypes) error {
return RegisterCustomKeyWithContext(nil, key, mask)
}
// RegisterCustomKeyWithContext registers a custom key with the context package.
func RegisterCustomKeyWithContext(
ctx types.Context, key interface{}, mask CustomKeyTypes) error {
customKeysRWL.Lock()
defer customKeysRWL.Unlock()
if _, ok := customKeys[key]; ok {
return goof.WithField("key", key, "key already registered")
}
newCustomKey := &customKey{
internalID: len(customKeys) + 1,
externalID: key,
keyBitmask: mask,
}
customKeys[newCustomKey.externalID] = newCustomKey
if ctx, ok := ctx.(*lsc); ok && ctx.logger != nil {
ctx.logger.WithFields(map[string]interface{}{
"internalID": newCustomKey.internalID,
"externalID": newCustomKey.externalID,
"keyBitmask": newCustomKey.keyBitmask,
}).Info("registered custom context key")
}
return nil
}
// CustomHeaderKeys returns a channel on which can be received all the
// registered, custom header keys.
func CustomHeaderKeys() <-chan interface{} {
c := make(chan interface{})
go func() {
customKeysRWL.RLock()
defer customKeysRWL.RUnlock()
for _, v := range customKeys {
if (v.keyBitmask & CustomHeaderKey) == CustomHeaderKey {
c <- v.externalID
}
}
close(c)
}()
return c
}
// CustomLoggerKeys returns a channel on which can be received all the
// registered, custom logger keys.
func CustomLoggerKeys() <-chan interface{} {
c := make(chan interface{})
go func() {
customKeysRWL.RLock()
defer customKeysRWL.RUnlock()
for _, v := range customKeys {
if (v.keyBitmask & CustomLoggerKey) == CustomLoggerKey {
c <- v.externalID
}
}
close(c)
}()
return c
}