-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddress.go
More file actions
141 lines (113 loc) · 3.07 KB
/
address.go
File metadata and controls
141 lines (113 loc) · 3.07 KB
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
package address
import "google.golang.org/grpc"
// ConnAddr is a map of addresses which refer to pointers to grpc.ClientConn
//
// This map is used to build dynamic connections for gRPC Loggers.
type ConnAddr map[string]*grpc.ClientConn
// New function will take in any number of addr and initialize a ConnAddr object with
// those addresses. Then, returns a pointer to this ConnAddr object.
func New(addr ...string) *ConnAddr {
if len(addr) == 0 {
return nil
}
var input []string
for _, a := range addr {
if a == "" {
continue
}
input = append(input, a)
}
if len(input) == 0 {
return nil
}
var a = &ConnAddr{}
a.Add(input...)
return a
}
// Map method will return a ConnAddr object in a map[string]*grpc.ClientConn format
func (a *ConnAddr) AsMap() map[string]*grpc.ClientConn {
return *a
}
// Add method will allocate the input strings as entries in the map, with initialized
// pointers to grpc.ClientConn
func (a *ConnAddr) Add(addr ...string) {
if len(addr) == 0 {
return
}
v := *a
for _, address := range addr {
if address == "" {
continue
}
if v[address] != nil {
continue
} else {
v[address] = &grpc.ClientConn{}
}
}
*a = v
}
// Keys method will return a ConnAddr object's keys (its addresses) in a slice of strings
func (a *ConnAddr) Keys() []string {
var keys []string
for k := range *a {
keys = append(keys, k)
}
return keys
}
// Get method will return the pointer to a grpc.ClientConn, as referenced in the input
// address k
func (a *ConnAddr) Get(k string) *grpc.ClientConn {
if a == nil || len(*a) == 0 {
return nil
}
v := *a
return v[k]
}
// Set method will allocate the input connection to the input string, within the ConnAddr
// map (overwritting it if existing)
func (a *ConnAddr) Set(k string, conn *grpc.ClientConn) {
v := *a
v[k] = conn
*a = v
}
// Len method will return the size of the ConnAddr map
func (a *ConnAddr) Len() int {
return len(*a)
}
// Reset method will overwrite the existing ConnAddr map with a new, empty one.
func (a *ConnAddr) Reset() {
if a.Len() == 0 {
return
}
new := ConnAddr(map[string]*grpc.ClientConn{})
*a = new
}
// Unset method will remove the input addr strings from the ConnAddr map, if existing
func (a *ConnAddr) Unset(addr ...string) {
if len(addr) == 0 {
return
}
v := *a
for _, address := range addr {
delete(v, address)
}
*a = v
}
// Write method is an implementation of io.Writer, so that the ConnAddr map can be used
// in a gRPC Logger's SetOuts() and AddOuts() methods. These need to conform with the
// Logger interface that implements these methods with a variatic number of io.Writer.
//
// For the same layer of compatibility to be possible in a gRPC Logger (who will write
// its log entries in a remote server), it uses these methods to implement its way of
// altering the existing connections, instead of dismissing this part of the implementation
// all together.
//
// ...That being said -- this is not any io.Writer.
func (a *ConnAddr) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return a.Len(), nil
}
a.Add(string(p))
return a.Len(), nil
}