...
1
17
18
19
20 package internal
21
22 import (
23 "encoding/json"
24 "fmt"
25
26 "google.golang.org/grpc/resolver"
27 )
28
29
30
31
32
33 type LocalityID struct {
34 Region string `json:"region,omitempty"`
35 Zone string `json:"zone,omitempty"`
36 SubZone string `json:"subZone,omitempty"`
37 }
38
39
40
41 func (l LocalityID) ToString() (string, error) {
42 b, err := json.Marshal(l)
43 if err != nil {
44 return "", err
45 }
46 return string(b), nil
47 }
48
49
50 func (l LocalityID) Equal(o any) bool {
51 ol, ok := o.(LocalityID)
52 if !ok {
53 return false
54 }
55 return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone
56 }
57
58
59
60 func LocalityIDFromString(s string) (ret LocalityID, _ error) {
61 err := json.Unmarshal([]byte(s), &ret)
62 if err != nil {
63 return LocalityID{}, fmt.Errorf("%s is not a well formatted locality ID, error: %v", s, err)
64 }
65 return ret, nil
66 }
67
68 type localityKeyType string
69
70 const localityKey = localityKeyType("grpc.xds.internal.address.locality")
71
72
73 func GetLocalityID(addr resolver.Address) LocalityID {
74 path, _ := addr.BalancerAttributes.Value(localityKey).(LocalityID)
75 return path
76 }
77
78
79 func SetLocalityID(addr resolver.Address, l LocalityID) resolver.Address {
80 addr.BalancerAttributes = addr.BalancerAttributes.WithValue(localityKey, l)
81 return addr
82 }
83
84
85 var ResourceTypeMapForTesting map[string]any
86
View as plain text