|
|
|
@ -6,7 +6,6 @@ import (
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
|
|
"github.com/go-zookeeper/zk"
|
|
|
|
@ -22,10 +21,10 @@ func (s *ZkClient) watch(wg *sync.WaitGroup) {
|
|
|
|
|
event := <-s.eventChan
|
|
|
|
|
switch event.Type {
|
|
|
|
|
case zk.EventSession:
|
|
|
|
|
log.ZDebug(context.Background(), "zk session event", "event", event)
|
|
|
|
|
s.logger.Printf("zk session event: %+v", event)
|
|
|
|
|
case zk.EventNodeCreated:
|
|
|
|
|
case zk.EventNodeChildrenChanged:
|
|
|
|
|
log.ZDebug(context.Background(), "zk event", "event", event.Path)
|
|
|
|
|
s.logger.Printf("zk event: %s", event.Path)
|
|
|
|
|
l := strings.Split(event.Path, "/")
|
|
|
|
|
if len(l) > 1 {
|
|
|
|
|
s.lock.Lock()
|
|
|
|
@ -35,9 +34,8 @@ func (s *ZkClient) watch(wg *sync.WaitGroup) {
|
|
|
|
|
delete(s.localConns, rpcName)
|
|
|
|
|
}
|
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
log.ZDebug(context.Background(), "zk event handle success", "event", event.Path)
|
|
|
|
|
s.logger.Printf("zk event handle success: %s", event.Path)
|
|
|
|
|
case zk.EventNodeDataChanged:
|
|
|
|
|
case zk.EventNodeDeleted:
|
|
|
|
|
case zk.EventNotWatching:
|
|
|
|
@ -74,13 +72,13 @@ func (s *ZkClient) GetConnsRemote(serviceName string) (conns []resolver.Address,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
|
|
|
|
|
log.ZDebug(ctx, "get conns from client", "serviceName", serviceName)
|
|
|
|
|
s.logger.Printf("get conns from client, serviceName: %s", serviceName)
|
|
|
|
|
s.lock.Lock()
|
|
|
|
|
opts = append(s.options, opts...)
|
|
|
|
|
conns := s.localConns[serviceName]
|
|
|
|
|
if len(conns) == 0 {
|
|
|
|
|
var err error
|
|
|
|
|
log.ZDebug(ctx, "get conns from zk remote", "serviceName", serviceName)
|
|
|
|
|
s.logger.Printf("get conns from zk remote, serviceName: %s", serviceName)
|
|
|
|
|
conns, err = s.GetConnsRemote(serviceName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.lock.Unlock()
|
|
|
|
@ -90,7 +88,7 @@ func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grp
|
|
|
|
|
}
|
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
var ret []*grpc.ClientConn
|
|
|
|
|
log.ZDebug(ctx, "get conns from zk success", "conns", conns)
|
|
|
|
|
s.logger.Printf("get conns from zk success, serviceName: %s", serviceName)
|
|
|
|
|
for _, conn := range conns {
|
|
|
|
|
c, err := grpc.DialContext(ctx, conn.Addr, append(s.options, opts...)...)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -98,14 +96,10 @@ func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grp
|
|
|
|
|
}
|
|
|
|
|
ret = append(ret, c)
|
|
|
|
|
}
|
|
|
|
|
log.ZDebug(ctx, "dial ctx success", "conns", ret)
|
|
|
|
|
s.logger.Printf("dial ctx success, serviceName: %s", serviceName)
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
|
|
|
|
// newOpts := append(s.options, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, s.balancerName)))
|
|
|
|
|
// return grpc.DialContext(ctx, fmt.Sprintf("%s:///%s", s.scheme, serviceName), append(newOpts, opts...)...)
|
|
|
|
|
// }
|
|
|
|
|
func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
|
|
|
|
conns, err := s.GetConns(ctx, serviceName, opts...)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -114,7 +108,7 @@ func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc
|
|
|
|
|
if len(conns) == 0 {
|
|
|
|
|
return nil, ErrConnIsNil
|
|
|
|
|
}
|
|
|
|
|
log.ZDebug(ctx, "get conn from conns", "conns", conns)
|
|
|
|
|
s.logger.Printf("get conn from conns, serviceName: %s", serviceName)
|
|
|
|
|
return s.getConnBalance(conns)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|