remove private AtomicInteger freeCount

This commit is contained in:
YuQing
2025-12-18 11:18:46 +08:00
parent d29ca63106
commit 7825aa0b94
6 changed files with 26 additions and 30 deletions
+1
View File
@@ -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
+1 -1
View File
@@ -4,7 +4,7 @@
<target name="init">
<property name="project.name" value="fastdfs-client-java"/>
<property name="project.version" value="1.38-SNAPSHOT"/>
<property name="project.version" value="1.39-SNAPSHOT"/>
<property name="project.java" value="${basedir}/src/main/java"/>
<property name="project.resources" value="${basedir}/src/main/resources"/>
<property name="project.build" value="${basedir}/build"/>
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.38-SNAPSHOT</version>
<version>1.39-SNAPSHOT</version>
<name>fastdfs-client-java</name>
<description>fastdfs client for java</description>
<packaging>jar</packaging>
@@ -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 +
'}';
}