guohao commented on code in PR #9950:
URL: https://github.com/apache/dubbo/pull/9950#discussion_r882387106


##########
dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/connection/DefaultConnectionPool.java:
##########
@@ -0,0 +1,252 @@
+/*
+ * 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 org.apache.dubbo.remoting.api.connection;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.remoting.api.Connection;
+import org.apache.dubbo.remoting.api.ConnectionPool;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class DefaultConnectionPool implements 
ConnectionPool<DefaultConnectionPoolEntry> {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultConnectionPool.class);
+
+    private volatile State state = State.ACTIVE;
+
+    private final int maxTotal = 8;
+
+    private final int maxIdle = 8;
+
+    private final int minIdle = 0;
+
+    private final CompletableFuture<Void> closeFuture = new 
CompletableFuture<>();
+
+    private final Queue<DefaultConnectionPoolEntry> cache = new 
ConcurrentLinkedQueue<>();
+
+    private final Queue<DefaultConnectionPoolEntry> all = new 
ConcurrentLinkedQueue<>();
+
+    private final AtomicInteger objectCount = new AtomicInteger();
+
+    private final AtomicInteger objectsInCreationCount = new AtomicInteger();
+
+    private final AtomicInteger idleCount = new AtomicInteger();
+
+    private final URL url;
+
+    public DefaultConnectionPool(URL url) {
+        this.url = url;
+    }
+
+
+    @Override
+    public ConnectionPoolEntry acquire() {
+        DefaultConnectionPoolEntry connection = cache.poll();

Review Comment:
   Thread not safe here



##########
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleInvoker.java:
##########
@@ -278,7 +287,14 @@ public boolean isAvailable() {
         if (!super.isAvailable()) {
             return false;
         }
-        return connection.isAvailable();
+        ConnectionPoolEntry poolEntry = connectionPool.acquire();
+        try {
+            connection = poolEntry.getConnection();
+            return connection.isAvailable();
+        } finally {
+            connectionPool.release(poolEntry);

Review Comment:
   Why release here?



##########
dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/DefaultConnectionPool.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * 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 org.apache.dubbo.remoting.api;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class DefaultConnectionPool implements ConnectionPool {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultConnectionPool.class);
+
+    private volatile State state = State.ACTIVE;
+
+    private final int maxTotal = 8;
+
+    private final int maxIdle = 8;
+
+    private final int minIdle = 0;
+
+    private final URL url;
+
+
+    private final CompletableFuture<Void> closeFuture = new 
CompletableFuture<>();
+
+    private final Queue<Connection> cache = new ConcurrentLinkedQueue<>();
+
+    private final Queue<Connection> all = new ConcurrentLinkedQueue<>();
+
+    private final AtomicInteger objectCount = new AtomicInteger();
+
+    private final AtomicInteger objectsInCreationCount = new AtomicInteger();
+
+    private final AtomicInteger idleCount = new AtomicInteger();
+
+    public DefaultConnectionPool(URL url) {
+        this.url = url;
+    }
+
+    @Override
+    public Connection acquire() {
+        Connection connection = cache.poll();
+        if (connection == null) {
+            long objects = getObjectCount() + getCreationInProgress();
+            if (getActualMaxTotal() > objects) {
+                return createConnection();
+            }
+            connection = all.peek();
+            connection.usedCount.incrementAndGet();
+            return connection;
+        }
+        idleCount.decrementAndGet();
+        return connection;
+    }
+
+    @Override
+    public void release(Connection connection) {
+        if (!all.contains(connection)) {
+            return;
+        }
+        if (connection.usedCount.decrementAndGet() > 0) {

Review Comment:
   race condion occurs when threads call `acquire` and `release` concurrently



##########
dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/connection/DefaultConnectionPool.java:
##########
@@ -0,0 +1,252 @@
+/*
+ * 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 org.apache.dubbo.remoting.api.connection;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.remoting.api.Connection;
+import org.apache.dubbo.remoting.api.ConnectionPool;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class DefaultConnectionPool implements 
ConnectionPool<DefaultConnectionPoolEntry> {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultConnectionPool.class);
+
+    private volatile State state = State.ACTIVE;
+
+    private final int maxTotal = 8;
+
+    private final int maxIdle = 8;
+
+    private final int minIdle = 0;
+
+    private final CompletableFuture<Void> closeFuture = new 
CompletableFuture<>();
+
+    private final Queue<DefaultConnectionPoolEntry> cache = new 
ConcurrentLinkedQueue<>();
+
+    private final Queue<DefaultConnectionPoolEntry> all = new 
ConcurrentLinkedQueue<>();
+
+    private final AtomicInteger objectCount = new AtomicInteger();
+
+    private final AtomicInteger objectsInCreationCount = new AtomicInteger();
+
+    private final AtomicInteger idleCount = new AtomicInteger();
+
+    private final URL url;
+
+    public DefaultConnectionPool(URL url) {
+        this.url = url;
+    }
+
+
+    @Override
+    public ConnectionPoolEntry acquire() {
+        DefaultConnectionPoolEntry connection = cache.poll();
+        if (connection == null) {
+            long objects = getObjectCount() + getCreationInProgress();
+            if (getActualMaxTotal() > objects) {
+                return createConnection();
+            }
+            connection = all.poll();
+            synchronized (connection.getReusedLock()) {
+                connection.getReusedCount().incrementAndGet();
+            }
+            try {
+                return connection;
+            } finally {
+                all.add(connection);
+            }
+        }
+        idleCount.decrementAndGet();
+        return connection;
+    }
+
+    @Override
+    public void release(DefaultConnectionPoolEntry poolEntry) {
+        if (poolEntry.getReusedCount().decrementAndGet() > 0) {

Review Comment:
   not thread safe



-- 
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: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to