feature: add socket connection pool
parent
3c61ba0e7a
commit
6aa61096b2
|
@ -147,4 +147,13 @@ public class TrackerServer {
|
||||||
public void setLastAccessTime(Long lastAccessTime) {
|
public void setLastAccessTime(Long lastAccessTime) {
|
||||||
this.lastAccessTime = lastAccessTime;
|
this.lastAccessTime = lastAccessTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "TrackerServer{" +
|
||||||
|
"sock=" + sock +
|
||||||
|
", inetSockAddr=" + inetSockAddr +
|
||||||
|
", lastAccessTime=" + lastAccessTime +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,4 +47,14 @@ public class ConnectionInfo {
|
||||||
this.lastAccessTime = lastAccessTime;
|
this.lastAccessTime = lastAccessTime;
|
||||||
this.needActiveCheck = needActiveCheck;
|
this.needActiveCheck = needActiveCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ConnectionInfo{" +
|
||||||
|
"socket=" + socket +
|
||||||
|
", inetSockAddr=" + inetSockAddr +
|
||||||
|
", lastAccessTime=" + lastAccessTime +
|
||||||
|
", needActiveCheck=" + needActiveCheck +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,12 +109,11 @@ public class ConnectionManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void freeConnection(TrackerServer trackerServer) throws IOException {
|
public void freeConnection(ConnectionInfo connectionInfo) throws IOException {
|
||||||
if (trackerServer == null || trackerServer.getSocket() == null) {
|
if (connectionInfo == null || connectionInfo.getSocket() == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true);
|
if ((System.currentTimeMillis() - connectionInfo.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) {
|
||||||
if ((System.currentTimeMillis() - trackerServer.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) {
|
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
freeConnections.add(connectionInfo);
|
freeConnections.add(connectionInfo);
|
||||||
|
|
|
@ -36,7 +36,11 @@ public class ConnectionPool {
|
||||||
String key = getKey(trackerServer.getInetSocketAddress());
|
String key = getKey(trackerServer.getInetSocketAddress());
|
||||||
if (key != null) {
|
if (key != null) {
|
||||||
ConnectionManager connectionManager = CP.get(key);
|
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 {
|
} else {
|
||||||
trackerServer.closeDirect();
|
trackerServer.closeDirect();
|
||||||
}
|
}
|
||||||
|
@ -49,7 +53,13 @@ public class ConnectionPool {
|
||||||
String key = getKey(trackerServer.getInetSocketAddress());
|
String key = getKey(trackerServer.getInetSocketAddress());
|
||||||
if (key != null) {
|
if (key != null) {
|
||||||
ConnectionManager connectionManager = CP.get(key);
|
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 {
|
} else {
|
||||||
trackerServer.closeDirect();
|
trackerServer.closeDirect();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue