Files
open-im-server/pkg/discoveryregistry/zookeeper/load_balancing.go
T

27 lines
410 B
Go
Raw Normal View History

2023-06-04 10:56:23 +08:00
package zookeeper
import (
"sync"
"google.golang.org/grpc"
)
type RoundRobin struct {
index int
lock sync.Mutex
}
2023-06-04 11:00:39 +08:00
func (r *RoundRobin) getConnBalance(conns []*grpc.ClientConn) (conn *grpc.ClientConn, err error) {
if len(conns) == 0 {
return nil, ErrConnIsNil
}
2023-06-04 10:56:23 +08:00
r.lock.Lock()
defer r.lock.Unlock()
if r.index < len(conns)-1 {
r.index++
} else {
r.index = 0
}
2023-06-04 11:00:39 +08:00
return conns[r.index], nil
2023-06-04 10:56:23 +08:00
}