diff --git a/HISTORY b/HISTORY index ea9fcb8..0a3a017 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ Version 1.39 2025-12-18 * ConnectionPool.getKey() use '-' instead of ':' + * ConnectionManager.java: remove private AtomicInteger freeCount Version 1.38 2025-11-28 * bugfixed: loadStorageServersFromTracker correctly with rw option diff --git a/build.xml b/build.xml index f328c62..27699fc 100644 --- a/build.xml +++ b/build.xml @@ -4,7 +4,7 @@ - + diff --git a/fastdfs-client.properties b/fastdfs-client.properties index 129d46b..e4cf380 100644 --- a/fastdfs-client.properties +++ b/fastdfs-client.properties @@ -21,4 +21,4 @@ fastdfs.connection_pool.max_count_per_entry = 500 fastdfs.connection_pool.max_idle_time = 3600 ## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000 -fastdfs.connection_pool.max_wait_time_in_ms = 1000 +fastdfs.connection_pool.max_wait_time_in_ms = 3000 diff --git a/fdfs_client.conf b/fdfs_client.conf index b58567e..a4a4af2 100644 --- a/fdfs_client.conf +++ b/fdfs_client.conf @@ -25,4 +25,4 @@ connect_first_by = tracker connection_pool.enabled = true connection_pool.max_count_per_entry = 500 connection_pool.max_idle_time = 3600 -connection_pool.max_wait_time_in_ms = 1000 +connection_pool.max_wait_time_in_ms = 3000 diff --git a/pom.xml b/pom.xml index e291567..bc01685 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.csource fastdfs-client-java - 1.38-SNAPSHOT + 1.39-SNAPSHOT fastdfs-client-java fastdfs client for java jar diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java index 0200bbe..44f7614 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java @@ -20,11 +20,6 @@ public class ConnectionManager { */ private AtomicInteger totalCount = new AtomicInteger(); - /** - * free connection count - */ - private AtomicInteger freeCount = new AtomicInteger(); - /** * lock */ @@ -50,9 +45,8 @@ public class ConnectionManager { try { Connection connection = null; while (true) { - if (freeCount.get() > 0) { - freeCount.decrementAndGet(); - connection = freeConnections.poll(); + connection = freeConnections.poll(); + if (connection != null) { if (!connection.isAvaliable() || (System.currentTimeMillis() - connection.getLastAccessTime()) > ClientGlobal.g_connection_pool_max_idle_time) { @@ -75,8 +69,8 @@ public class ConnectionManager { connection.setNeedActiveTest(false); } } - } else if (ClientGlobal.g_connection_pool_max_count_per_entry == 0 || totalCount.get() < - ClientGlobal.g_connection_pool_max_count_per_entry) + } else if (ClientGlobal.g_connection_pool_max_count_per_entry == 0 || + totalCount.get() < ClientGlobal.g_connection_pool_max_count_per_entry) { connection = ConnectionFactory.create(this.inetSocketAddress); totalCount.incrementAndGet(); @@ -86,12 +80,16 @@ public class ConnectionManager { //wait single success continue; } - throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() - + ":" + inetSocketAddress.getPort() + " fail, wait_time > " - + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms"); + + throw new MyException("connections reach max_count_per_entry: " + + ClientGlobal.g_connection_pool_max_count_per_entry + ", " + + "await connection for server " + inetSocketAddress.getAddress().getHostAddress() + + ":" + inetSocketAddress.getPort() + " timeout, wait_time > " + + ClientGlobal.g_connection_pool_max_wait_time_in_ms + " ms"); } catch (InterruptedException e) { e.printStackTrace(); - throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + throw new MyException("connection full, await connection for server " + + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg: " + e.getMessage()); } } @@ -110,7 +108,6 @@ public class ConnectionManager { try { connection.setLastAccessTime(System.currentTimeMillis()); freeConnections.add(connection); - freeCount.incrementAndGet(); condition.signal(); } finally { lock.unlock(); @@ -124,31 +121,29 @@ public class ConnectionManager { connection.closeDirectly(); } } catch (IOException e) { - System.err.println("close socket[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] error, emsg: " + e.getMessage()); + System.err.println("close socket[" + inetSocketAddress.getAddress().getHostAddress() + + ":" + inetSocketAddress.getPort() + "] error, emsg: " + e.getMessage()); e.printStackTrace(); } } public void setActiveTestFlag() { - if (freeCount.get() > 0) { - lock.lock(); - try { - for (Connection freeConnection : freeConnections) { - freeConnection.setNeedActiveTest(true); - } - } finally { - lock.unlock(); + lock.lock(); + try { + for (Connection freeConnection : freeConnections) { + freeConnection.setNeedActiveTest(true); } + } finally { + lock.unlock(); } } - @Override public String toString() { return "ConnectionManager{" + "ip:port='" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + ", totalCount=" + totalCount + - ", freeCount=" + freeCount + + ", freeCount=" + freeConnections.size() + ", freeConnections =" + freeConnections + '}'; }