-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceId.go
60 lines (48 loc) · 1.45 KB
/
serviceId.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
package grpc
import (
"fmt"
"strconv"
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
)
const (
mdKeyServiceId = "mdkey_serviceid"
)
// WithServiceId returns a copy of outgoing context, with service ID as part of
// the metadata.
// If the outgoing context already has metadata, the metadata will be joined.
func WithServiceId(outgoing context.Context, serviceId int) context.Context {
md := metadata.Pairs(mdKeyServiceId, strconv.Itoa(serviceId))
// Pass existing metadata down.
if existingMd, ok := metadata.FromOutgoingContext(outgoing); ok {
md = metadata.Join(existingMd, md)
}
return metadata.NewOutgoingContext(outgoing, md)
}
// GetServiceId gets service ID from incoming context metadata.
func GetServiceId(incoming context.Context) (int, error) {
val, err := getLastVal(incoming, mdKeyServiceId)
if err != nil {
return -1, err
}
id, err := strconv.ParseInt(val, 10, 0)
if err != nil {
return -1, err
}
return int(id), nil
}
func getLastVal(incoming context.Context, key string) (string, error) {
md, ok := metadata.FromIncomingContext(incoming)
if !ok {
return "", fmt.Errorf("Failed to get metadata from incoming context")
}
vals, found := md[key]
if !found {
return "", fmt.Errorf("Failed to find key %q from metadata", key)
}
if len(vals) == 0 {
return "", fmt.Errorf("Found empty list of values for key %q in metadata", key)
}
// Returns the last value, which is the latest.
return vals[len(vals)-1], nil
}