kylixs commented on a change in pull request #9015:
URL: https://github.com/apache/dubbo/pull/9015#discussion_r728799336



##########
File path: 
dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/zookeeper/AbstractZookeeperTransporter.java
##########
@@ -54,25 +58,51 @@ public ZookeeperClient connect(URL url) {
         ZookeeperClient zookeeperClient;
         // address format: {[username:password@]address}
         List<String> addressList = getURLBackupAddress(url);
-        // The field define the zookeeper server , including protocol, host, 
port, username, password
-        if ((zookeeperClient = 
fetchAndUpdateZookeeperClientCache(addressList)) != null && 
zookeeperClient.isConnected()) {
-            logger.info("find valid zookeeper client from the cache for 
address: " + url);
-            return zookeeperClient;
-        }
+        String application = url.getParameter(APPLICATION_KEY, "");
         // avoid creating too many connections, so add lock
-        synchronized (zookeeperClientMap) {
-            if ((zookeeperClient = 
fetchAndUpdateZookeeperClientCache(addressList)) != null && 
zookeeperClient.isConnected()) {
+        synchronized (zookeeperApplicationMap) {
+            if ((zookeeperClient = 
fetchAndUpdateZookeeperClientCache(addressList, application)) != null &&
+                    zookeeperClient.isConnected()) {
                 logger.info("find valid zookeeper client from the cache for 
address: " + url);
                 return zookeeperClient;
             }
 
             zookeeperClient = createZookeeperClient(url);
+            Set<String> applications = new HashSet<>();
+            applications.add(application);
+            zookeeperApplicationMap.put(zookeeperClient, applications);        
    
             logger.info("No valid zookeeper client found from cache, therefore 
create a new client for url. " + url);
             writeToClientMap(addressList, zookeeperClient);
         }
         return zookeeperClient;
     }
 
+    /**
+     * close zookeeper connection if no application use it.
+     * 
+     * @param zookeeperClient
+     * @param application the application which is destroying. 
+     */
+    @Override
+    public void close(ZookeeperClient zookeeperClient, String application) {
+        synchronized (zookeeperApplicationMap) {
+            Set<String> applications = 
zookeeperApplicationMap.get(zookeeperClient);
+            if (applications == null) {
+                logger.warn("No applications associated with the zookeeper 
client: " + zookeeperClient.getUrl());
+                zookeeperClient.close();
+                return;
+            }
+
+            if (application == null) {
+                application = "";
+            }
+            applications.remove(application);
+            if (application.isEmpty()) {
+                zookeeperClient.close();

Review comment:
       Remove  zookeeperClient from zookeeperApplicationMap after close it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to