diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java index 7c5a927..68edb9b 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java @@ -13,7 +13,7 @@ import java.util.concurrent.locks.ReentrantLock; public class ConnectionManager { /** - * ip_port + * ip:port is key */ private String key; @@ -23,7 +23,7 @@ public class ConnectionManager { private AtomicInteger totalCount = new AtomicInteger(); /** - * freeCP + * free connection count */ private AtomicInteger freeCount = new AtomicInteger(); @@ -32,13 +32,10 @@ public class ConnectionManager { */ private ReentrantLock lock = new ReentrantLock(true); - /** - * condition - */ private Condition condition = lock.newCondition(); /** - * connection container + * free connections */ private volatile ConcurrentLinkedQueue freeConnections = new ConcurrentLinkedQueue(); @@ -109,11 +106,12 @@ public class ConnectionManager { } } - public void freeConnection(ConnectionInfo connectionInfo) throws IOException { - if (connectionInfo == null || connectionInfo.getSocket() == null) { + public void freeConnection(TrackerServer trackerServer) throws IOException { + if (trackerServer == null || !trackerServer.isConnected()) { return; } - if ((System.currentTimeMillis() - connectionInfo.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) { + ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true); + if ((System.currentTimeMillis() - trackerServer.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) { try { lock.lock(); freeConnections.add(connectionInfo); @@ -132,7 +130,7 @@ public class ConnectionManager { totalCount.decrementAndGet(); try { ProtoCommon.closeSocket(connectionInfo.getSocket()); - } finally { + } finally { connectionInfo.setSocket(null); } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java index 62813c6..c31465b 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java @@ -54,8 +54,7 @@ public class ConnectionPool { if (key != null) { ConnectionManager connectionManager = CP.get(key); if (connectionManager != null) { - ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true); - connectionManager.freeConnection(connectionInfo); + connectionManager.freeConnection(trackerServer); } else { trackerServer.closeDirect(); }