-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_discovery.go
309 lines (261 loc) · 7.26 KB
/
register_discovery.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
package discovery
import (
"context"
"github.com/RealFax/RedQueen/client"
"github.com/RealFax/red-discovery/internal/hack"
"github.com/RealFax/red-discovery/internal/maputil"
"github.com/google/uuid"
"google.golang.org/grpc"
"sync"
"time"
)
type ListenCallbackFunc func(ready bool, conn *grpc.ClientConn, wg *sync.WaitGroup)
type DiscoveryAndRegister interface {
// ReleaseDiscovery cancel the discovery of a Naming.
ReleaseDiscovery(naming string)
// Discovery a Naming, will open a goroutine to achieve continuous discovery of Naming.
Discovery(naming string) error
// Unregister one or more services using an Endpoint ID.
Unregister(naming string, ids ...string) error
// Register one or more services with Naming.
Register(naming string, endpoints ...*Endpoint) error
// UseListener
//
// Monitors whether a Naming is available.
// When ready is true, it means that there are available services for this Naming.
// When it is false, it means that there are no services available under the Naming.
UseListener(naming string, callback ListenCallbackFunc) (string, error)
// DestroyListener cancel listening to Naming based on the ListenerID returned by UseListener.
DestroyListener(naming, listenerID string)
}
type discoveryAndRegister struct {
ctx context.Context
dialOpts []grpc.DialOption
kvClient client.KvClient
services *maputil.Map[string, Service] // map<naming, Service>
discovery *maputil.Map[string, context.CancelFunc] // map<naming, discoverySignal>
listener *maputil.Map[string, *maputil.Map[string, ListenCallbackFunc]] // map<string, map<string, ListenCallbackFunc>>
}
func (r *discoveryAndRegister) notifyStateChange(srv Service) {
childListener, found := r.listener.Load(srv.Naming())
if !found {
return
}
var (
wg = &sync.WaitGroup{}
state = srv.Alive()
conn, _ = srv.NextAliveConn()
)
childListener.Range(func(_ string, fc ListenCallbackFunc) bool {
wg.Add(1)
fc(state, conn, wg)
return true
})
go func() {
wg.Wait()
}()
}
func (r *discoveryAndRegister) discoveryDaemon(namespace *string, naming string) {
// double-check-area naming discovery status
ctx, cancel := context.WithCancel(r.ctx)
// release naming discovery lock
// defer cancel()
// discovery has existed
if _, ok := r.discovery.LoadOrStore(naming, cancel); ok {
return
}
watcher := client.NewWatcher(
hack.String2Bytes(naming),
client.WatchWithPrefix(),
client.WatchWithNamespace(namespace),
)
defer watcher.Close()
go func() {
defer func() {
// prevent variable race
_cancel, ok := r.discovery.LoadAndDelete(naming)
if !ok {
return
}
_cancel()
}()
// async watch
if err := r.kvClient.WatchPrefix(ctx, watcher); err != nil {
return
}
}()
// get watcher notify
var (
ok bool
err error
endpointNaming, endpointID string
value *client.WatchValue
endpoint *Endpoint
srv Service
notify, _ = watcher.Notify()
)
for {
select {
case <-ctx.Done():
return
case value = <-notify:
if value == nil {
continue
}
if endpointNaming, endpointID, err = ParseEndpointPath(hack.Bytes2String(value.Key)); err != nil {
continue
}
// trying load exist service, if not found and value not nil then init service
if srv, ok = r.services.Load(endpointNaming); !ok {
srv, ok = r.services.LoadOrStore(endpointNaming, NewService(r.ctx, endpointNaming, r.dialOpts...))
if !ok {
srv, _ = r.services.Load(endpointNaming)
}
}
// deleted
if value.Value == nil {
srv.DelEndpoints(endpointID)
// notify naming listeners
r.notifyStateChange(srv)
continue
}
if endpoint, err = ParseEndpoint(value.Value); err != nil {
continue
}
endpoint.lastUpdated = value.Timestamp
endpoint.SetTTL(value.TTL)
// update endpoint
srv.AddEndpoints(endpoint)
// notify naming listeners
r.notifyStateChange(srv)
}
}
}
func (r *discoveryAndRegister) ReleaseDiscovery(naming string) {
cancel, ok := r.discovery.LoadAndDelete(naming)
if !ok {
return
}
cancel()
}
func (r *discoveryAndRegister) Discovery(naming string) error {
// check discovery status
if exist := r.discovery.Exist(naming); exist {
if !r.services.Exist(naming) {
r.services.LoadOrStore(naming, NewService(r.ctx, naming, r.dialOpts...))
}
return ErrDiscoveryHasExist
}
go r.discoveryDaemon(namespace.Load(), naming)
values, err := r.kvClient.PrefixScan(
r.ctx,
hack.String2Bytes(naming),
0,
MaxEndpointSize,
nil,
namespace.Load(),
)
if err != nil {
return nil
}
srv, ok := r.services.Load(naming)
if !ok {
srv = NewService(r.ctx, naming, r.dialOpts...)
r.services.Store(naming, srv)
}
var endpoint *Endpoint
for _, value := range values {
if endpoint, err = ParseEndpoint(value.Data); err != nil {
return err
}
endpoint.SetTTL(value.TTL)
endpoint.lastUpdated = time.Now().UnixMilli()
srv.AddEndpoints(endpoint)
}
return nil
}
func (r *discoveryAndRegister) Unregister(naming string, ids ...string) (err error) {
srv, ok := r.services.Load(naming)
if !ok {
return ErrServiceNotExist
}
for _, id := range ids {
if err = r.kvClient.Delete(
r.ctx,
hack.String2Bytes(srv.WithEndpointNaming(id)),
namespace.Load(),
); err != nil {
return
}
// del endpoint from service
srv.DelEndpoints(id)
}
return
}
func (r *discoveryAndRegister) Register(naming string, endpoints ...*Endpoint) (err error) {
srv, ok := r.services.Load(naming)
if !ok {
srv = NewService(r.ctx, naming, r.dialOpts...)
r.services.Store(naming, srv)
}
var endpointOut []byte
// registered endpoints
for _, endpoint := range endpoints {
endpointOut, _ = endpoint.Marshal()
if err = r.kvClient.Set(
r.ctx,
hack.String2Bytes(endpoint.WithNaming(naming)),
endpointOut,
endpoint.TTL(),
namespace.Load(),
); err != nil {
continue
}
// add endpoint to service
srv.AddEndpoints(endpoint)
}
return
}
func (r *discoveryAndRegister) UseListener(naming string, callback ListenCallbackFunc) (string, error) {
if exist := r.discovery.Exist(naming); !exist {
return "", ErrShouldDiscoveryFirst
}
var (
found bool
childListener *maputil.Map[string, ListenCallbackFunc]
)
if childListener, found = r.listener.Load(naming); !found {
childListener, found = r.listener.LoadOrStore(naming, maputil.New[string, ListenCallbackFunc]())
if !found {
childListener, _ = r.listener.Load(naming)
}
}
listenerID := uuid.NewString()
childListener.Store(listenerID, callback)
return listenerID, nil
}
func (r *discoveryAndRegister) DestroyListener(naming, listenerID string) {
if exist := r.discovery.Exist(naming); !exist {
return
}
childListener, found := r.listener.Load(naming)
if !found {
return
}
childListener.Delete(listenerID)
}
func NewDiscoveryAndRegister(
ctx context.Context,
services *maputil.Map[string, Service],
c *client.Client,
dialOpts ...grpc.DialOption,
) DiscoveryAndRegister {
return &discoveryAndRegister{
ctx: ctx,
dialOpts: dialOpts,
kvClient: c,
services: services,
discovery: maputil.New[string, context.CancelFunc](),
listener: maputil.New[string, *maputil.Map[string, ListenCallbackFunc]](),
}
}