mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-03 02:42:19 +08:00
feat: multiple address
This commit is contained in:
parent
898dde9bb8
commit
faed7dc345
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/OpenIMSDK/tools/errs"
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
|
"github.com/OpenIMSDK/tools/log"
|
||||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -98,6 +99,7 @@ func (cm *ConnManager) GetConns(ctx context.Context,
|
|||||||
func (cm *ConnManager) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
func (cm *ConnManager) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||||
// Check if there are existing connections for the service
|
// Check if there are existing connections for the service
|
||||||
if conns, exists := cm.conns[serviceName]; exists && len(conns) > 0 {
|
if conns, exists := cm.conns[serviceName]; exists && len(conns) > 0 {
|
||||||
|
log.ZDebug(ctx, "getConn", "len(conns)", len(conns))
|
||||||
// Return a random connection from the existing connections
|
// Return a random connection from the existing connections
|
||||||
randomIndex := rand.Intn(len(conns))
|
randomIndex := rand.Intn(len(conns))
|
||||||
return conns[randomIndex], nil
|
return conns[randomIndex], nil
|
||||||
@ -106,6 +108,7 @@ func (cm *ConnManager) GetConn(ctx context.Context, serviceName string, opts ...
|
|||||||
// Get service addresses
|
// Get service addresses
|
||||||
addresses := getServiceAddresses()
|
addresses := getServiceAddresses()
|
||||||
address, ok := addresses[serviceName]
|
address, ok := addresses[serviceName]
|
||||||
|
log.ZDebug(ctx, "getConn address", "address", address)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errs.Wrap(errors.New("unknown service name"), "serviceName", serviceName)
|
return nil, errs.Wrap(errors.New("unknown service name"), "serviceName", serviceName)
|
||||||
}
|
}
|
||||||
@ -113,7 +116,7 @@ func (cm *ConnManager) GetConn(ctx context.Context, serviceName string, opts ...
|
|||||||
// Try to dial a new connection
|
// Try to dial a new connection
|
||||||
conn, err := dialService(ctx, fmt.Sprintf(config2.Config.Rpc.ListenIP+":%d", address[randomIndex]), append(cm.additionalOpts, opts...)...)
|
conn, err := dialService(ctx, fmt.Sprintf(config2.Config.Rpc.ListenIP+":%d", address[randomIndex]), append(cm.additionalOpts, opts...)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errs.Wrap(err, "address", fmt.Sprintf(config2.Config.Rpc.ListenIP+":%d", address[randomIndex]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the new connection
|
// Store the new connection
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user