forked from SkySoft-ATM/gorillaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd.go
271 lines (236 loc) · 6.93 KB
/
sd.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
package gorillaz
import (
"context"
"errors"
"fmt"
"go.uber.org/zap"
"google.golang.org/grpc/resolver"
"strings"
"sync"
"time"
)
const SdPrefix = "sd://"
type ServiceDefinition struct {
ServiceName string
Addr string
Port int
Tags []string
Meta map[string]string
}
type ServiceDiscovery interface {
Register(d *ServiceDefinition) (RegistrationHandle, error)
Resolve(serviceName string) ([]ServiceDefinition, error)
ResolveWithTag(serviceName, tag string) ([]ServiceDefinition, error)
ResolveTags(tag string) (map[string][]ServiceDefinition, error)
}
type RegistrationHandle interface {
DeRegister(context.Context) error
}
// gorillazResolverBuilder is a
// ResolverBuilder(https://godoc.org/google.golang.org/grpc/resolver#Builder).
type gorillazResolverBuilder struct {
gaz *Gaz
}
func (g *gorillazResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
var result resolver.Resolver
if strings.HasPrefix(target.Endpoint, SdPrefix) {
if g.gaz.ServiceDiscovery == nil {
return nil, errors.New("service discovery not initialized in gorillaz")
}
r := &serviceDiscoveryResolver{
serviceDiscovery: g.gaz.ServiceDiscovery,
name: strings.TrimPrefix(target.Endpoint, SdPrefix),
cc: cc,
closeChan: make(chan struct{}),
tick: time.NewTicker(1 * time.Second),
env: g.gaz.Env,
}
go r.updater()
result = r
} else {
split := strings.Split(target.Endpoint, ",")
addrs := make([]resolver.Address, len(split))
for i, s := range split {
addrs[i] = resolver.Address{Addr: s}
}
r := &gorillazDefaultResolver{
cc: cc,
addresses: addrs,
}
r.start()
result = r
}
return result, nil
}
func (*gorillazResolverBuilder) Scheme() string { return "gorillaz" }
// serviceDiscoveryResolver is a
// Resolver(https://godoc.org/google.golang.org/grpc/resolver#Resolver).
type serviceDiscoveryResolver struct {
closeChan chan struct{}
serviceDiscovery ServiceDiscovery
name string
cc resolver.ClientConn
tick *time.Ticker
env string
}
func (r *serviceDiscoveryResolver) updater() {
r.sendUpdate()
for {
select {
case <-r.tick.C:
r.sendUpdate()
case <-r.closeChan:
r.tick.Stop()
select {
// just in case, consume any pending tick
case <-r.tick.C:
// nothing to do
default:
// ok
}
return
}
}
}
func (r *serviceDiscoveryResolver) sendUpdate() {
endpoints, err := r.serviceDiscovery.ResolveWithTag(r.name, r.env)
if err != nil {
Log.Warn("Error while resolving ", zap.String("name", r.name), zap.Error(err))
return
}
addrs := make([]resolver.Address, len(endpoints))
for i, e := range endpoints {
addrs[i] = resolver.Address{Addr: fmt.Sprintf("%s:%d", e.Addr, e.Port)}
}
r.cc.UpdateState(resolver.State{Addresses: addrs})
}
func (*serviceDiscoveryResolver) ResolveNow(o resolver.ResolveNowOptions) {}
func (r *serviceDiscoveryResolver) Close() {
select {
case r.closeChan <- struct{}{}:
// nothing
default:
Log.Warn("serviceDiscoveryResolver is already closed")
}
}
// gorillazDefaultResolver is a
// Resolver(https://godoc.org/google.golang.org/grpc/resolver#Resolver).
type gorillazDefaultResolver struct {
cc resolver.ClientConn
addresses []resolver.Address
}
func (r *gorillazDefaultResolver) start() {
r.cc.UpdateState(resolver.State{Addresses: r.addresses})
}
func (*gorillazDefaultResolver) ResolveNow(o resolver.ResolveNowOptions) {}
func (*gorillazDefaultResolver) Close() {}
func (g *Gaz) Register(d *ServiceDefinition) (RegistrationHandle, error) {
if g.ServiceDiscovery == nil {
return nil, errors.New("no service registry configured")
}
return g.ServiceDiscovery.Register(d)
}
func (g *Gaz) Resolve(serviceName string) ([]ServiceDefinition, error) {
return g.ServiceDiscovery.Resolve(serviceName)
}
func (g *Gaz) ResolveWithTag(serviceName, tag string) ([]ServiceDefinition, error) {
return g.ServiceDiscovery.ResolveWithTag(serviceName, tag)
}
func NewMockedServiceDiscovery() (*MockedServiceDiscovery, Option) {
return NewMockedServiceDiscoveryWithDefinitions([]ServiceDefinition{})
}
func NewMockedServiceDiscoveryWithDefinitions(serviceDefs []ServiceDefinition) (*MockedServiceDiscovery, Option) {
mock := &MockedServiceDiscovery{mocked: serviceDefs}
return mock, Option{Opt: func(gaz *Gaz) error {
mock.mu.Lock()
mock.g = gaz
mock.mu.Unlock()
gaz.ServiceDiscovery = mock
return nil
}}
}
type MockedServiceDiscovery struct {
g *Gaz
mu sync.RWMutex
mocked []ServiceDefinition
}
func (m *MockedServiceDiscovery) UpdateGaz(g *Gaz) {
m.mu.Lock()
defer m.mu.Unlock()
m.g = g
}
func (m *MockedServiceDiscovery) MockService(defs []ServiceDefinition) {
m.mu.Lock()
defer m.mu.Unlock()
m.mocked = append(m.mocked, defs...)
}
type MockedRegistrationHandle struct{}
func (m MockedRegistrationHandle) DeRegister(ctx context.Context) error {
return nil
}
func (m *MockedServiceDiscovery) Register(d *ServiceDefinition) (RegistrationHandle, error) {
return MockedRegistrationHandle{}, nil
}
func (m *MockedServiceDiscovery) Resolve(serviceName string) ([]ServiceDefinition, error) {
m.mu.RLock()
defer m.mu.RUnlock()
res := make([]ServiceDefinition, 0)
for _, mock := range m.mocked {
if mock.ServiceName == serviceName {
res = append(res, mock)
}
}
if len(res) > 0 {
return res, nil
}
result := ServiceDefinition{
ServiceName: serviceName,
Addr: "localhost",
Port: m.g.GrpcPort(),
Tags: []string{},
Meta: map[string]string{},
}
Sugar.Debugf("Service %s with mocked to local gRPC server on port %d", serviceName, m.g.GrpcPort())
return []ServiceDefinition{result}, nil
}
func (m *MockedServiceDiscovery) ResolveWithTag(serviceName, tag string) ([]ServiceDefinition, error) {
m.mu.RLock()
defer m.mu.RUnlock()
res := make([]ServiceDefinition, 0)
for _, mock := range m.mocked {
if mock.ServiceName == serviceName && contains(mock.Tags, tag) {
res = append(res, mock)
}
}
if len(res) > 0 {
return res, nil
}
result := ServiceDefinition{
ServiceName: serviceName,
Addr: "localhost",
Port: m.g.GrpcPort(),
Tags: []string{tag},
Meta: map[string]string{},
}
Sugar.Debugf("Service %s with tag %s mocked to local gRPC server on port %d", serviceName, tag, m.g.GrpcPort())
return []ServiceDefinition{result}, nil
}
func contains(slice []string, e string) bool {
for _, i := range slice {
if i == e {
return true
}
}
return false
}
func (m *MockedServiceDiscovery) ResolveTags(tag string) (map[string][]ServiceDefinition, error) {
return nil, errors.New("unimplemented")
}
func WithMockedServiceDiscovery() Option {
_, r := NewMockedServiceDiscovery()
return r
}
func WithMockedServiceDiscoveryDefinitions(definitions []ServiceDefinition) Option {
_, r := NewMockedServiceDiscoveryWithDefinitions(definitions)
return r
}