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

yizhenqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 7039c98  Optimize the close and connect operations of AbstractClient 
(#3435)
7039c98 is described below

commit 7039c98b6cb5a0b69edc4e9e82679131da1a8643
Author: yizhenqiang <[email protected]>
AuthorDate: Thu Oct 15 18:12:41 2020 +0800

    Optimize the close and connect operations of AbstractClient (#3435)
    
    * The AbstractClient close operation is the same as the connect and 
disconnect operations, and is protected by the lock, which reduces the 
probability of uncertainty due to operation conflicts; the connect operation 
first determines the close state of the client. If the client is closed, the 
client does not need to be reconnected.
    
    * The judgment conditions of strengthening connect and close methods
    
    Co-authored-by: Xin Wang <[email protected]>
---
 .../dubbo/remoting/transport/AbstractClient.java   | 74 ++++++++++++++--------
 1 file changed, 47 insertions(+), 27 deletions(-)

diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java
 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java
index 8f6bf61..a4d59cc 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java
+++ 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java
@@ -66,6 +66,7 @@ public abstract class AbstractClient extends AbstractEndpoint 
implements Client
                     "Failed to start " + getClass().getSimpleName() + " " + 
NetUtils.getLocalAddress()
                             + " connect to the server " + getRemoteAddress() + 
", cause: " + t.getMessage(), t);
         }
+
         try {
             // connect.
             connect();
@@ -179,27 +180,31 @@ public abstract class AbstractClient extends 
AbstractEndpoint implements Client
     }
 
     protected void connect() throws RemotingException {
-
         connectLock.lock();
 
         try {
-
             if (isConnected()) {
                 return;
             }
 
+            if (isClosed() || isClosing()) {
+                logger.warn("No need to connect to server " + 
getRemoteAddress() + " from " + getClass().getSimpleName() + " "
+                        + NetUtils.getLocalHost() + " using dubbo version " + 
Version.getVersion() + ", cause: client status is closed or closing.");
+                return;
+            }
+
             doConnect();
 
             if (!isConnected()) {
                 throw new RemotingException(this, "Failed connect to server " 
+ getRemoteAddress() + " from " + getClass().getSimpleName() + " "
-                        + NetUtils.getLocalHost() + " using dubbo version " + 
Version.getVersion()
-                        + ", cause: Connect wait timeout: " + 
getConnectTimeout() + "ms.");
+                                + NetUtils.getLocalHost() + " using dubbo 
version " + Version.getVersion()
+                                + ", cause: Connect wait timeout: " + 
getConnectTimeout() + "ms.");
 
             } else {
                 if (logger.isInfoEnabled()) {
                     logger.info("Successed connect to server " + 
getRemoteAddress() + " from " + getClass().getSimpleName() + " "
-                            + NetUtils.getLocalHost() + " using dubbo version 
" + Version.getVersion()
-                            + ", channel is " + this.getChannel());
+                                    + NetUtils.getLocalHost() + " using dubbo 
version " + Version.getVersion()
+                                    + ", channel is " + this.getChannel());
                 }
             }
 
@@ -208,8 +213,8 @@ public abstract class AbstractClient extends 
AbstractEndpoint implements Client
 
         } catch (Throwable e) {
             throw new RemotingException(this, "Failed connect to server " + 
getRemoteAddress() + " from " + getClass().getSimpleName() + " "
-                    + NetUtils.getLocalHost() + " using dubbo version " + 
Version.getVersion()
-                    + ", cause: " + e.getMessage(), e);
+                            + NetUtils.getLocalHost() + " using dubbo version 
" + Version.getVersion()
+                            + ", cause: " + e.getMessage(), e);
 
         } finally {
             connectLock.unlock();
@@ -254,31 +259,46 @@ public abstract class AbstractClient extends 
AbstractEndpoint implements Client
 
     @Override
     public void close() {
-
-        try {
-            super.close();
-        } catch (Throwable e) {
-            logger.warn(e.getMessage(), e);
+        if (isClosed()) {
+            logger.warn("No need to close connection to server " + 
getRemoteAddress() + " from " + getClass().getSimpleName() + " " + 
NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", 
cause: the client status is closed.");
+            return;
         }
 
+        connectLock.lock();
         try {
-            if (executor != null) {
-                ExecutorUtil.shutdownNow(executor, 100);
+            if (isClosed()) {
+                logger.warn("No need to close connection to server " + 
getRemoteAddress() + " from " + getClass().getSimpleName() + " " + 
NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", 
cause: the client status is closed.");
+                return;
             }
-        } catch (Throwable e) {
-            logger.warn(e.getMessage(), e);
-        }
 
-        try {
-            disconnect();
-        } catch (Throwable e) {
-            logger.warn(e.getMessage(), e);
-        }
+            try {
+                super.close();
+            } catch (Throwable e) {
+                logger.warn(e.getMessage(), e);
+            }
 
-        try {
-            doClose();
-        } catch (Throwable e) {
-            logger.warn(e.getMessage(), e);
+            try {
+                if (executor != null) {
+                    ExecutorUtil.shutdownNow(executor, 100);
+                }
+            } catch (Throwable e) {
+                logger.warn(e.getMessage(), e);
+            }
+
+            try {
+                disconnect();
+            } catch (Throwable e) {
+                logger.warn(e.getMessage(), e);
+            }
+
+            try {
+                doClose();
+            } catch (Throwable e) {
+                logger.warn(e.getMessage(), e);
+            }
+
+        } finally {
+            connectLock.unlock();
         }
     }
 

Reply via email to