fix:connection pool

dev
tanyawen 2019-12-28 05:18:42 +08:00
parent 6aa61096b2
commit 006af0379f
2 changed files with 9 additions and 12 deletions

View File

@ -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<ConnectionInfo> freeConnections = new ConcurrentLinkedQueue<ConnectionInfo>();
@ -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);
}
}

View File

@ -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();
}