/
info.go
329 lines (288 loc) · 8.13 KB
/
info.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2021 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metainfo
import (
"context"
)
// The prefix listed below may be used to tag the types of values when there is no context to carry them.
const (
PrefixPersistent = "RPC_PERSIST_"
PrefixTransient = "RPC_TRANSIT_"
PrefixTransientUpstream = "RPC_TRANSIT_UPSTREAM_"
PrefixBackward = "RPC_BACKWARD_"
PrefixBackwardDownstream = "RPC_BACKWARD_DOWNSTREAM_"
lenPTU = len(PrefixTransientUpstream)
lenPT = len(PrefixTransient)
lenPP = len(PrefixPersistent)
lenB = len(PrefixBackward)
lenBD = len(PrefixBackwardDownstream)
)
// **Using empty string as key or value is not support.**
// TransferForward converts transient values to transient-upstream values and filters out original transient-upstream values.
// It should be used before the context is passing from server to client.
func TransferForward(ctx context.Context) context.Context {
if n := getNode(ctx); n != nil {
return withNode(ctx, n.transferForward())
}
return ctx
}
// GetValue retrieves the value set into the context by the given key.
func GetValue(ctx context.Context, k string) (v string, ok bool) {
if n := getNode(ctx); n != nil {
if idx, ok := search(n.transient, k); ok {
return n.transient[idx].val, true
}
if idx, ok := search(n.stale, k); ok {
return n.stale[idx].val, true
}
}
return
}
// GetAllValues retrieves all transient values
func GetAllValues(ctx context.Context) (m map[string]string) {
if n := getNode(ctx); n != nil {
if cnt := len(n.stale) + len(n.transient); cnt > 0 {
m = make(map[string]string, cnt)
for _, kv := range n.stale {
m[kv.key] = kv.val
}
for _, kv := range n.transient {
m[kv.key] = kv.val
}
}
}
return
}
// RangeValues calls f sequentially for each transient kv.
// If f returns false, range stops the iteration.
func RangeValues(ctx context.Context, f func(k, v string) bool) {
n := getNode(ctx)
if n == nil {
return
}
if cnt := len(n.stale) + len(n.transient); cnt == 0 {
return
}
for _, kv := range n.stale {
if !f(kv.key, kv.val) {
return
}
}
for _, kv := range n.transient {
if !f(kv.key, kv.val) {
return
}
}
}
// WithValue sets the value into the context by the given key.
// This value will be propagated to the next service/endpoint through an RPC call.
//
// Notice that it will not propagate any further beyond the next service/endpoint,
// Use WithPersistentValue if you want to pass a key/value pair all the way.
func WithValue(ctx context.Context, k, v string) context.Context {
if len(k) == 0 || len(v) == 0 {
return ctx
}
if n := getNode(ctx); n != nil {
if m := n.addTransient(k, v); m != n {
return withNode(ctx, m)
}
} else {
return withNode(ctx, &node{
transient: []kv{{key: k, val: v}},
})
}
return ctx
}
// DelValue deletes a key/value from the current context.
// Since empty string value is not valid, we could just set the value to be empty.
func DelValue(ctx context.Context, k string) context.Context {
if len(k) == 0 {
return ctx
}
if n := getNode(ctx); n != nil {
if m := n.delTransient(k); m != n {
return withNode(ctx, m)
}
}
return ctx
}
// GetPersistentValue retrieves the persistent value set into the context by the given key.
func GetPersistentValue(ctx context.Context, k string) (v string, ok bool) {
if n := getNode(ctx); n != nil {
if idx, ok := search(n.persistent, k); ok {
return n.persistent[idx].val, true
}
}
return
}
// GetAllPersistentValues retrieves all persistent values.
func GetAllPersistentValues(ctx context.Context) (m map[string]string) {
if n := getNode(ctx); n != nil {
if cnt := len(n.persistent); cnt > 0 {
m = make(map[string]string, cnt)
for _, kv := range n.persistent {
m[kv.key] = kv.val
}
}
}
return
}
// RangePersistentValues calls f sequentially for each persistent kv.
// If f returns false, range stops the iteration.
func RangePersistentValues(ctx context.Context, f func(k, v string) bool) {
n := getNode(ctx)
if n == nil {
return
}
for _, kv := range n.persistent {
if !f(kv.key, kv.val) {
break
}
}
}
// WithPersistentValue sets the value into the context by the given key.
// This value will be propagated to the services along the RPC call chain.
func WithPersistentValue(ctx context.Context, k, v string) context.Context {
if len(k) == 0 || len(v) == 0 {
return ctx
}
if n := getNode(ctx); n != nil {
if m := n.addPersistent(k, v); m != n {
return withNode(ctx, m)
}
} else {
return withNode(ctx, &node{
persistent: []kv{{key: k, val: v}},
})
}
return ctx
}
// DelPersistentValue deletes a persistent key/value from the current context.
// Since empty string value is not valid, we could just set the value to be empty.
func DelPersistentValue(ctx context.Context, k string) context.Context {
if len(k) == 0 {
return ctx
}
if n := getNode(ctx); n != nil {
if m := n.delPersistent(k); m != n {
return withNode(ctx, m)
}
}
return ctx
}
func getKey(kvs []string, i int) string {
return kvs[i*2]
}
func getValue(kvs []string, i int) string {
return kvs[i*2+1]
}
// CountPersistentValues counts the length of persisten KV pairs
func CountPersistentValues(ctx context.Context) int {
if n := getNode(ctx); n == nil {
return 0
} else {
return len(n.persistent)
}
}
// CountValues counts the length of transient KV pairs
func CountValues(ctx context.Context) int {
if n := getNode(ctx); n == nil {
return 0
} else {
return len(n.stale) + len(n.transient)
}
}
// WithPersistentValues sets the values into the context by the given keys.
// This value will be propagated to the services along the RPC call chain.
func WithPersistentValues(ctx context.Context, kvs ...string) context.Context {
if len(kvs)%2 != 0 {
panic("len(kvs) must be even")
}
kvLen := len(kvs) / 2
if ctx == nil || len(kvs) == 0 {
return ctx
}
var n *node
if m := getNode(ctx); m != nil {
nn := *m
n = &nn
n.persistent = make([]kv, len(m.persistent), len(m.persistent)+kvLen)
copy(n.persistent, m.persistent)
} else {
n = &node{
persistent: make([]kv, 0, kvLen),
}
}
for i := 0; i < kvLen; i++ {
key := getKey(kvs, i)
val := getValue(kvs, i)
if len(key) == 0 || len(val) == 0 {
continue
}
if idx, ok := search(n.persistent, key); ok {
if n.persistent[idx].val != val {
n.persistent[idx].val = val
}
} else {
n.persistent = append(n.persistent, kv{key: key, val: val})
}
}
return withNode(ctx, n)
}
// WithValue sets the values into the context by the given keys.
// This value will be propagated to the next service/endpoint through an RPC call.
//
// Notice that it will not propagate any further beyond the next service/endpoint,
// Use WithPersistentValues if you want to pass key/value pairs all the way.
func WithValues(ctx context.Context, kvs ...string) context.Context {
if len(kvs)%2 != 0 {
panic("len(kvs) must be even")
}
kvLen := len(kvs) / 2
if ctx == nil || len(kvs) == 0 {
return ctx
}
var n *node
if m := getNode(ctx); m != nil {
nn := *m
n = &nn
n.transient = make([]kv, len(m.transient), len(m.transient)+kvLen)
copy(n.transient, m.transient)
} else {
n = &node{
transient: make([]kv, 0, kvLen),
}
}
for i := 0; i < kvLen; i++ {
key := getKey(kvs, i)
val := getValue(kvs, i)
if len(key) == 0 || len(val) == 0 {
continue
}
if res, ok := remove(n.stale, key); ok {
n.stale = res
n.transient = append(n.transient, kv{key: key, val: val})
continue
}
if idx, ok := search(n.transient, key); ok {
if n.transient[idx].val != val {
n.transient[idx].val = val
}
} else {
n.transient = append(n.transient, kv{key: key, val: val})
}
}
return withNode(ctx, n)
}