feature: add socket connection pool

dev
tanyawen 2019-12-28 01:43:42 +08:00
parent 3c61ba0e7a
commit 6aa61096b2
4 changed files with 34 additions and 6 deletions

View File

@ -147,4 +147,13 @@ public class TrackerServer {
public void setLastAccessTime(Long lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}
@Override
public String toString() {
return "TrackerServer{" +
"sock=" + sock +
", inetSockAddr=" + inetSockAddr +
", lastAccessTime=" + lastAccessTime +
'}';
}
}

View File

@ -47,4 +47,14 @@ public class ConnectionInfo {
this.lastAccessTime = lastAccessTime;
this.needActiveCheck = needActiveCheck;
}
@Override
public String toString() {
return "ConnectionInfo{" +
"socket=" + socket +
", inetSockAddr=" + inetSockAddr +
", lastAccessTime=" + lastAccessTime +
", needActiveCheck=" + needActiveCheck +
'}';
}
}

View File

@ -109,12 +109,11 @@ public class ConnectionManager {
}
}
public void freeConnection(TrackerServer trackerServer) throws IOException {
if (trackerServer == null || trackerServer.getSocket() == null) {
public void freeConnection(ConnectionInfo connectionInfo) throws IOException {
if (connectionInfo == null || connectionInfo.getSocket() == null) {
return;
}
ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true);
if ((System.currentTimeMillis() - trackerServer.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) {
if ((System.currentTimeMillis() - connectionInfo.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) {
try {
lock.lock();
freeConnections.add(connectionInfo);

View File

@ -36,7 +36,11 @@ public class ConnectionPool {
String key = getKey(trackerServer.getInetSocketAddress());
if (key != null) {
ConnectionManager connectionManager = CP.get(key);
connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true));
if (connectionManager != null) {
connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true));
} else {
trackerServer.closeDirect();
}
} else {
trackerServer.closeDirect();
}
@ -49,7 +53,13 @@ public class ConnectionPool {
String key = getKey(trackerServer.getInetSocketAddress());
if (key != null) {
ConnectionManager connectionManager = CP.get(key);
connectionManager.freeConnection(trackerServer);
if (connectionManager != null) {
ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true);
connectionManager.freeConnection(connectionInfo);
} else {
trackerServer.closeDirect();
}
} else {
trackerServer.closeDirect();
}