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

alexstocks 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 d7e5177  fix(getty): fix nil panic when set default getty config 
(#1437)
d7e5177 is described below

commit d7e51774dd2eec27d77cb72a3370db645d65f325
Author: Mulavar <[email protected]>
AuthorDate: Sat Sep 4 16:11:45 2021 +0800

    fix(getty): fix nil panic when set default getty config (#1437)
    
    * fix(getty): fix nil panic when set default getty config
    
    * style(getty): go fmt
    
    * style(getty): add license header
    
    Co-authored-by: dongjianhui03 <[email protected]>
---
 remoting/getty/config.go            | 12 +++++++----
 remoting/getty/getty_client.go      |  9 ++++----
 remoting/getty/getty_client_test.go | 18 ++++++++++++++++
 remoting/getty/getty_server.go      | 10 +++++----
 remoting/getty/getty_server_test.go | 41 +++++++++++++++++++++++++++++++++++++
 5 files changed, 78 insertions(+), 12 deletions(-)

diff --git a/remoting/getty/config.go b/remoting/getty/config.go
index 5cf36bf..234fa86 100644
--- a/remoting/getty/config.go
+++ b/remoting/getty/config.go
@@ -109,8 +109,8 @@ type (
 )
 
 // GetDefaultClientConfig gets client default configuration
-func GetDefaultClientConfig() ClientConfig {
-       return ClientConfig{
+func GetDefaultClientConfig() *ClientConfig {
+       defaultClientConfig := &ClientConfig{
                ReconnectInterval: 0,
                ConnectionNum:     16,
                HeartbeatPeriod:   "30s",
@@ -132,11 +132,13 @@ func GetDefaultClientConfig() ClientConfig {
                        SessionName:      "client",
                },
        }
+       _ = defaultClientConfig.CheckValidity()
+       return defaultClientConfig
 }
 
 // GetDefaultServerConfig gets server default configuration
-func GetDefaultServerConfig() ServerConfig {
-       return ServerConfig{
+func GetDefaultServerConfig() *ServerConfig {
+       defaultServerConfig := &ServerConfig{
                SessionTimeout: "180s",
                SessionNumber:  700,
                GrPoolSize:     120,
@@ -156,6 +158,8 @@ func GetDefaultServerConfig() ServerConfig {
                        SessionName:      "server",
                },
        }
+       _ = defaultServerConfig.CheckValidity()
+       return defaultServerConfig
 }
 
 // CheckValidity confirm getty session params
diff --git a/remoting/getty/getty_client.go b/remoting/getty/getty_client.go
index 226e33e..d6ca10d 100644
--- a/remoting/getty/getty_client.go
+++ b/remoting/getty/getty_client.go
@@ -49,12 +49,14 @@ var (
        errClientClosed      = perrors.New("client closed")
        errClientReadTimeout = perrors.New("maybe the client read timeout or 
fail to decode tcp stream in Writer.Write")
 
-       clientConf   *ClientConfig
+       clientConf *ClientConfig
+
        clientGrPool gxsync.GenericTaskPool
 )
 
 // it is init client for single protocol.
 func initClient(protocol string) {
+       clientConf = GetDefaultClientConfig()
        if protocol == "" {
                return
        }
@@ -69,9 +71,9 @@ func initClient(protocol string) {
        }
 
        protocolConf := config.GetRootConfig().Protocols[protocol]
-       defaultClientConfig := GetDefaultClientConfig()
        if protocolConf == nil {
                logger.Info("use default getty client config")
+               return
        } else {
                gettyClientConfig := protocolConf.Params
                if gettyClientConfig == nil {
@@ -82,12 +84,11 @@ func initClient(protocol string) {
                if err != nil {
                        panic(err)
                }
-               err = yaml.Unmarshal(gettyClientConfigBytes, 
&defaultClientConfig)
+               err = yaml.Unmarshal(gettyClientConfigBytes, clientConf)
                if err != nil {
                        panic(err)
                }
        }
-       clientConf = &defaultClientConfig
        if err := clientConf.CheckValidity(); err != nil {
                logger.Warnf("[CheckValidity] error: %v", err)
                return
diff --git a/remoting/getty/getty_client_test.go 
b/remoting/getty/getty_client_test.go
index cef1197..2820681 100644
--- a/remoting/getty/getty_client_test.go
+++ b/remoting/getty/getty_client_test.go
@@ -38,6 +38,7 @@ import (
        "dubbo.apache.org/dubbo-go/v3/common"
        . "dubbo.apache.org/dubbo-go/v3/common/constant"
        "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory"
+       "dubbo.apache.org/dubbo-go/v3/config"
        "dubbo.apache.org/dubbo-go/v3/protocol"
        "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
        "dubbo.apache.org/dubbo-go/v3/remoting"
@@ -476,3 +477,20 @@ func (u *UserProvider) Reference() string {
 func (u User) JavaClassName() string {
        return "com.ikurento.user.User"
 }
+
+func TestInitClient(t *testing.T) {
+       originRootConf := config.GetRootConfig()
+       rootConf := config.RootConfig{
+               Protocols: map[string]*config.ProtocolConfig{
+                       "dubbo": {
+                               Name: "dubbo",
+                               Ip:   "127.0.0.1",
+                               Port: "20003",
+                       },
+               },
+       }
+       config.SetRootConfig(rootConf)
+       initServer("dubbo")
+       config.SetRootConfig(*originRootConf)
+       assert.NotNil(t, srvConf)
+}
diff --git a/remoting/getty/getty_server.go b/remoting/getty/getty_server.go
index f048bd7..e53f0d4 100644
--- a/remoting/getty/getty_server.go
+++ b/remoting/getty/getty_server.go
@@ -43,9 +43,12 @@ import (
        "dubbo.apache.org/dubbo-go/v3/remoting"
 )
 
-var srvConf *ServerConfig
+var (
+       srvConf *ServerConfig
+)
 
 func initServer(protocol string) {
+       srvConf = GetDefaultServerConfig()
        if protocol == "" {
                return
        }
@@ -60,9 +63,9 @@ func initServer(protocol string) {
        }
 
        protocolConf := config.GetRootConfig().Protocols[protocol]
-       defaultServerConfig := GetDefaultServerConfig()
        if protocolConf == nil {
                logger.Info("use default getty server config")
+               return
        } else {
                gettyServerConfig := protocolConf.Params
                if gettyServerConfig == nil {
@@ -74,12 +77,11 @@ func initServer(protocol string) {
                if err != nil {
                        panic(err)
                }
-               err = yaml.Unmarshal(gettyServerConfigBytes, 
&defaultServerConfig)
+               err = yaml.Unmarshal(gettyServerConfigBytes, srvConf)
                if err != nil {
                        panic(err)
                }
        }
-       srvConf = &defaultServerConfig
        if err := srvConf.CheckValidity(); err != nil {
                panic(err)
        }
diff --git a/remoting/getty/getty_server_test.go 
b/remoting/getty/getty_server_test.go
new file mode 100644
index 0000000..1f08199
--- /dev/null
+++ b/remoting/getty/getty_server_test.go
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package getty
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/config"
+       "github.com/stretchr/testify/assert"
+       "testing"
+)
+
+func TestInitServer(t *testing.T) {
+       originRootConf := config.GetRootConfig()
+       rootConf := config.RootConfig{
+               Protocols: map[string]*config.ProtocolConfig{
+                       "dubbo": {
+                               Name: "dubbo",
+                               Ip:   "127.0.0.1",
+                               Port: "20003",
+                       },
+               },
+       }
+       config.SetRootConfig(rootConf)
+       initServer("dubbo")
+       config.SetRootConfig(*originRootConf)
+       assert.NotNil(t, srvConf)
+}

Reply via email to