From b0899b7a96f81e091eba3b40e7794d40073e0b92 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sat, 20 Nov 2021 18:57:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DRPC=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E7=AE=A1=E7=90=86=E6=B2=A1=E6=9C=89=E5=8A=A0=E9=94=81?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/rpc/rpc_client.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index 1deb114..84c30cc 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -25,7 +25,7 @@ type RPCClient struct { apiConfig *configs.APIConfig conns []*grpc.ClientConn - locker sync.Mutex + locker sync.RWMutex } func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { @@ -177,15 +177,23 @@ func (this *RPCClient) ClusterContext(clusterId string, clusterSecret string) co // Close 关闭连接 func (this *RPCClient) Close() { + this.locker.Lock() + for _, conn := range this.conns { _ = conn.Close() } + + this.locker.Unlock() } // UpdateConfig 修改配置 func (this *RPCClient) UpdateConfig(config *configs.APIConfig) error { this.apiConfig = config - return this.init() + + this.locker.Lock() + err := this.init() + this.locker.Unlock() + return err } // 初始化 @@ -223,8 +231,8 @@ func (this *RPCClient) init() error { // 随机选择一个连接 func (this *RPCClient) pickConn() *grpc.ClientConn { - this.locker.Lock() - defer this.locker.Unlock() + this.locker.RLock() + defer this.locker.RUnlock() // 检查连接状态 if len(this.conns) > 0 {