mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
conn
This commit is contained in:
parent
37e8dbfb94
commit
7051c06c09
@ -1,4 +1,4 @@
|
||||
package openKeeper
|
||||
package zookeeper
|
||||
|
||||
import (
|
||||
"github.com/go-zookeeper/zk"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package openKeeper
|
||||
package zookeeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -107,7 +107,7 @@ func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc
|
||||
if len(conns) == 0 {
|
||||
return nil, ErrConnIsNil
|
||||
}
|
||||
return conns[0], nil
|
||||
return s.getConnBalance(conns), nil
|
||||
}
|
||||
|
||||
func (s *ZkClient) GetFirstConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||
|
23
pkg/discoveryregistry/zookeeper/load_balancing.go
Normal file
23
pkg/discoveryregistry/zookeeper/load_balancing.go
Normal file
@ -0,0 +1,23 @@
|
||||
package zookeeper
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type RoundRobin struct {
|
||||
index int
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (r *RoundRobin) getConnBalance(conns []*grpc.ClientConn) (conn *grpc.ClientConn) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
if r.index < len(conns)-1 {
|
||||
r.index++
|
||||
} else {
|
||||
r.index = 0
|
||||
}
|
||||
return conns[r.index]
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package openKeeper
|
||||
package zookeeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -38,6 +38,7 @@ type ZkClient struct {
|
||||
resolvers map[string]*Resolver
|
||||
localConns map[string][]resolver.Address
|
||||
balancerName string
|
||||
RoundRobin
|
||||
}
|
||||
|
||||
type ZkOption func(*ZkClient)
|
||||
|
Loading…
x
Reference in New Issue
Block a user