@@ -5,10 +5,12 @@ import (
5
5
"fmt"
6
6
"io"
7
7
"net"
8
+ "strconv"
8
9
"strings"
9
10
"sync"
10
11
"time"
11
12
13
+ "github.com/openimsdk/tools/discovery"
12
14
"github.com/openimsdk/tools/errs"
13
15
"github.com/openimsdk/tools/log"
14
16
"github.com/openimsdk/tools/utils/datautil"
@@ -191,7 +193,7 @@ func (r *SvcDiscoveryRegistryImpl) GetUserIdHashGatewayHost(ctx context.Context,
191
193
}
192
194
193
195
// GetConns returns gRPC client connections for a given service name
194
- func (r * SvcDiscoveryRegistryImpl ) GetConns (ctx context.Context , serviceName string , opts ... grpc.DialOption ) ([]* grpc.ClientConn , error ) {
196
+ func (r * SvcDiscoveryRegistryImpl ) GetConns (ctx context.Context , serviceName string , opts ... grpc.DialOption ) ([]grpc.ClientConnInterface , error ) {
195
197
fullServiceKey := fmt .Sprintf ("%s/%s" , r .rootDirectory , serviceName )
196
198
if len (r .connMap ) == 0 {
197
199
if err := r .initializeConnMap (opts ... ); err != nil {
@@ -200,11 +202,11 @@ func (r *SvcDiscoveryRegistryImpl) GetConns(ctx context.Context, serviceName str
200
202
}
201
203
r .mu .RLock ()
202
204
defer r .mu .RUnlock ()
203
- return datautil .Batch (func (t * addrConn ) * grpc.ClientConn { return t .conn }, r .connMap [fullServiceKey ]), nil
205
+ return datautil .Batch (func (t * addrConn ) grpc.ClientConnInterface { return t .conn }, r .connMap [fullServiceKey ]), nil
204
206
}
205
207
206
208
// GetConn returns a single gRPC client connection for a given service name
207
- func (r * SvcDiscoveryRegistryImpl ) GetConn (ctx context.Context , serviceName string , opts ... grpc.DialOption ) (* grpc.ClientConn , error ) {
209
+ func (r * SvcDiscoveryRegistryImpl ) GetConn (ctx context.Context , serviceName string , opts ... grpc.DialOption ) (grpc.ClientConnInterface , error ) {
208
210
target := fmt .Sprintf ("etcd:///%s/%s" , r .rootDirectory , serviceName )
209
211
210
212
dialOpts := append (append (r .dialOptions , opts ... ), grpc .WithResolvers (r .resolver ))
@@ -222,6 +224,14 @@ func (r *SvcDiscoveryRegistryImpl) GetSelfConnTarget() string {
222
224
return r .rpcRegisterTarget
223
225
}
224
226
227
+ func (r * SvcDiscoveryRegistryImpl ) IsSelfNode (cc grpc.ClientConnInterface ) bool {
228
+ cli , ok := cc .(* grpc.ClientConn )
229
+ if ! ok {
230
+ return false
231
+ }
232
+ return r .GetSelfConnTarget () == cli .Target ()
233
+ }
234
+
225
235
// AddOption appends gRPC dial options to the existing options
226
236
func (r * SvcDiscoveryRegistryImpl ) AddOption (opts ... grpc.DialOption ) {
227
237
r .mu .Lock ()
@@ -231,20 +241,20 @@ func (r *SvcDiscoveryRegistryImpl) AddOption(opts ...grpc.DialOption) {
231
241
}
232
242
233
243
// CloseConn closes a given gRPC client connection
234
- func (r * SvcDiscoveryRegistryImpl ) CloseConn (conn * grpc.ClientConn ) {
235
- conn .Close ()
236
- }
244
+ // func (r *SvcDiscoveryRegistryImpl) CloseConn(conn *grpc.ClientConn) {
245
+ // conn.Close()
246
+ // }
237
247
238
248
// Register registers a new service endpoint with etcd
239
- func (r * SvcDiscoveryRegistryImpl ) Register (serviceName , host string , port int , opts ... grpc.DialOption ) error {
240
- r .serviceKey = fmt .Sprintf ("%s/%s/%s:%d " , r .rootDirectory , serviceName , host , port )
249
+ func (r * SvcDiscoveryRegistryImpl ) Register (ctx context. Context , serviceName , host string , port int , opts ... grpc.DialOption ) error {
250
+ r .serviceKey = fmt .Sprintf ("%s/%s/%s" , r .rootDirectory , serviceName , net . JoinHostPort ( host , strconv . Itoa ( port )) )
241
251
em , err := endpoints .NewManager (r .client , r .rootDirectory + "/" + serviceName )
242
252
if err != nil {
243
253
return err
244
254
}
245
255
r .endpointMgr = em
246
256
247
- leaseResp , err := r .client .Grant (context . Background () , 30 ) //
257
+ leaseResp , err := r .client .Grant (ctx , 30 ) //
248
258
if err != nil {
249
259
return err
250
260
}
@@ -259,6 +269,12 @@ func (r *SvcDiscoveryRegistryImpl) Register(serviceName, host string, port int,
259
269
}
260
270
261
271
go r .keepAliveLease (r .leaseID )
272
+
273
+ //_, err := r.client.Put(ctx, BuildDiscoveryKey(serviceName), jsonutil.StructToJsonString(BuildDefaultTarget(host, port)))
274
+ //if err != nil {
275
+ // return err
276
+ //}
277
+
262
278
return nil
263
279
}
264
280
@@ -405,3 +421,42 @@ func (r *SvcDiscoveryRegistryImpl) resetConnMap() {
405
421
}
406
422
r .connMap = make (map [string ][]* addrConn )
407
423
}
424
+
425
+ func (r * SvcDiscoveryRegistryImpl ) SetKey (ctx context.Context , key string , data []byte ) error {
426
+ if _ , err := r .client .Put (ctx , key , string (data )); err != nil {
427
+ return errs .WrapMsg (err , "etcd put err" )
428
+ }
429
+ return nil
430
+ }
431
+
432
+ func (r * SvcDiscoveryRegistryImpl ) GetKey (ctx context.Context , key string ) ([]byte , error ) {
433
+ resp , err := r .client .Get (ctx , key )
434
+ if err != nil {
435
+ return nil , errs .WrapMsg (err , "etcd get err" )
436
+ }
437
+ if len (resp .Kvs ) == 0 {
438
+ return nil , nil
439
+ }
440
+ return resp .Kvs [0 ].Value , nil
441
+ }
442
+
443
+ func (r * SvcDiscoveryRegistryImpl ) DelData (ctx context.Context , key string ) error {
444
+ if _ , err := r .client .Delete (ctx , key ); err != nil {
445
+ return errs .WrapMsg (err , "etcd delete err" )
446
+ }
447
+ return nil
448
+ }
449
+
450
+ func (r * SvcDiscoveryRegistryImpl ) WatchKey (ctx context.Context , key string , fn discovery.WatchKeyHandler ) error {
451
+ watchChan := r .client .Watch (ctx , key )
452
+ for watchResp := range watchChan {
453
+ for _ , event := range watchResp .Events {
454
+ if event .IsModify () && string (event .Kv .Key ) == key {
455
+ if err := fn (& discovery.WatchKey {Value : event .Kv .Value }); err != nil {
456
+ return err
457
+ }
458
+ }
459
+ }
460
+ }
461
+ return nil
462
+ }
0 commit comments