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 { public class ConnectionManager {
/** /**
* ip_port * ip:port is key
*/ */
private String key; private String key;
@ -23,7 +23,7 @@ public class ConnectionManager {
private AtomicInteger totalCount = new AtomicInteger(); private AtomicInteger totalCount = new AtomicInteger();
/** /**
* freeCP * free connection count
*/ */
private AtomicInteger freeCount = new AtomicInteger(); private AtomicInteger freeCount = new AtomicInteger();
@ -32,13 +32,10 @@ public class ConnectionManager {
*/ */
private ReentrantLock lock = new ReentrantLock(true); private ReentrantLock lock = new ReentrantLock(true);
/**
* condition
*/
private Condition condition = lock.newCondition(); private Condition condition = lock.newCondition();
/** /**
* connection container * free connections
*/ */
private volatile ConcurrentLinkedQueue<ConnectionInfo> freeConnections = new ConcurrentLinkedQueue<ConnectionInfo>(); private volatile ConcurrentLinkedQueue<ConnectionInfo> freeConnections = new ConcurrentLinkedQueue<ConnectionInfo>();
@ -109,11 +106,12 @@ public class ConnectionManager {
} }
} }
public void freeConnection(ConnectionInfo connectionInfo) throws IOException { public void freeConnection(TrackerServer trackerServer) throws IOException {
if (connectionInfo == null || connectionInfo.getSocket() == null) { if (trackerServer == null || !trackerServer.isConnected()) {
return; 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 { try {
lock.lock(); lock.lock();
freeConnections.add(connectionInfo); freeConnections.add(connectionInfo);
@ -132,7 +130,7 @@ public class ConnectionManager {
totalCount.decrementAndGet(); totalCount.decrementAndGet();
try { try {
ProtoCommon.closeSocket(connectionInfo.getSocket()); ProtoCommon.closeSocket(connectionInfo.getSocket());
} finally { } finally {
connectionInfo.setSocket(null); connectionInfo.setSocket(null);
} }
} }

View File

@ -54,8 +54,7 @@ public class ConnectionPool {
if (key != null) { if (key != null) {
ConnectionManager connectionManager = CP.get(key); ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) { if (connectionManager != null) {
ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true); connectionManager.freeConnection(trackerServer);
connectionManager.freeConnection(connectionInfo);
} else { } else {
trackerServer.closeDirect(); trackerServer.closeDirect();
} }