You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by la...@apache.org on 2021/09/07 06:44:58 UTC

[dubbo-go] branch config-enhance updated: Fixes : grpc client init problem #1416 (#1418)

This is an automated email from the ASF dual-hosted git repository.

laurence pushed a commit to branch config-enhance
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/config-enhance by this push:
     new 78ca90e  Fixes : grpc client init problem  #1416 (#1418)
78ca90e is described below

commit 78ca90e296a1577c569957a776eec7af9b472c82
Author: WilliamLeaves <wi...@gmail.com>
AuthorDate: Tue Sep 7 14:44:51 2021 +0800

    Fixes : grpc client init problem  #1416 (#1418)
    
    * fix grpc client init problem
    
    * add once
    
    Co-authored-by: yexiaobo <ye...@luojilab.com>
---
 protocol/grpc/client.go | 88 ++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 42 deletions(-)

diff --git a/protocol/grpc/client.go b/protocol/grpc/client.go
index 1d37eb1..60a51dd 100644
--- a/protocol/grpc/client.go
+++ b/protocol/grpc/client.go
@@ -20,6 +20,7 @@ package grpc
 import (
 	"reflect"
 	"strconv"
+	"sync"
 	"time"
 )
 
@@ -41,48 +42,7 @@ import (
 )
 
 var clientConf *ClientConfig
-
-func init() {
-	// load rootConfig from runtime
-	rootConfig := config.GetRootConfig()
-
-	clientConfig := GetClientConfig()
-	clientConf = &clientConfig
-
-	// check client config and decide whether to use the default config
-	defer func() {
-		if clientConf == nil || len(clientConf.ContentSubType) == 0 {
-			defaultClientConfig := GetDefaultClientConfig()
-			clientConf = &defaultClientConfig
-		}
-		if err := clientConf.Validate(); err != nil {
-			panic(err)
-		}
-	}()
-
-	if rootConfig.Application == nil {
-		return
-	}
-	protocolConf := config.GetRootConfig().Protocols
-
-	if protocolConf == nil {
-		logger.Info("protocol_conf default use dubbo config")
-	} else {
-		grpcConf := protocolConf[GRPC]
-		if grpcConf == nil {
-			logger.Warnf("grpcConf is nil")
-			return
-		}
-		grpcConfByte, err := yaml.Marshal(grpcConf)
-		if err != nil {
-			panic(err)
-		}
-		err = yaml.Unmarshal(grpcConfByte, clientConf)
-		if err != nil {
-			panic(err)
-		}
-	}
-}
+var clientConfInitOnce sync.Once
 
 // Client is gRPC client include client connection and invoker
 type Client struct {
@@ -92,6 +52,8 @@ type Client struct {
 
 // NewClient creates a new gRPC client.
 func NewClient(url *common.URL) (*Client, error) {
+	clientConfInitOnce.Do(clientInit)
+
 	// If global trace instance was set, it means trace function enabled.
 	// If not, will return NoopTracer.
 	tracer := opentracing.GlobalTracer()
@@ -131,6 +93,48 @@ func NewClient(url *common.URL) (*Client, error) {
 	}, nil
 }
 
+func clientInit() {
+	// load rootConfig from runtime
+	rootConfig := config.GetRootConfig()
+
+	clientConfig := GetClientConfig()
+	clientConf = &clientConfig
+
+	// check client config and decide whether to use the default config
+	defer func() {
+		if clientConf == nil || len(clientConf.ContentSubType) == 0 {
+			defaultClientConfig := GetDefaultClientConfig()
+			clientConf = &defaultClientConfig
+		}
+		if err := clientConf.Validate(); err != nil {
+			panic(err)
+		}
+	}()
+
+	if rootConfig.Application == nil {
+		return
+	}
+	protocolConf := config.GetRootConfig().Protocols
+
+	if protocolConf == nil {
+		logger.Info("protocol_conf default use dubbo config")
+	} else {
+		grpcConf := protocolConf[GRPC]
+		if grpcConf == nil {
+			logger.Warnf("grpcConf is nil")
+			return
+		}
+		grpcConfByte, err := yaml.Marshal(grpcConf)
+		if err != nil {
+			panic(err)
+		}
+		err = yaml.Unmarshal(grpcConfByte, clientConf)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 func getInvoker(impl interface{}, conn *grpc.ClientConn) interface{} {
 	var in []reflect.Value
 	in = append(in, reflect.ValueOf(conn))