Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
withchao 2023-06-04 10:58:57 +08:00
commit 212c692228
6 changed files with 30 additions and 6 deletions

View File

@ -1,4 +1,4 @@
package openKeeper
package zookeeper
import (
"github.com/go-zookeeper/zk"

View File

@ -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) {

View 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]
}

View File

@ -1,4 +1,4 @@
package openKeeper
package zookeeper
import (
"github.com/go-zookeeper/zk"

View File

@ -1,4 +1,4 @@
package openKeeper
package zookeeper
import (
"context"

View File

@ -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)