Merge branch 'feature_add_connect_pool_20191205'

dev
YuQing 2020-01-01 14:45:04 +08:00
commit 06f5624a8d
3 changed files with 43 additions and 1 deletions

View File

@ -15,6 +15,8 @@ public class Connection {
private InetSocketAddress inetSockAddr;
private Long lastAccessTime = System.currentTimeMillis();
private boolean needActiveTest = false;
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
this.sock = sock;
this.inetSockAddr = inetSockAddr;
@ -117,11 +119,21 @@ public class Connection {
return false;
}
public boolean isNeedActiveTest() {
return needActiveTest;
}
public void setNeedActiveTest(boolean needActiveTest) {
this.needActiveTest = needActiveTest;
}
@Override
public String toString() {
return "TrackerServer{" +
return "Connection{" +
"sock=" + sock +
", inetSockAddr=" + inetSockAddr +
", lastAccessTime=" + lastAccessTime +
", needActiveTest=" + needActiveTest +
'}';
}
}

View File

@ -57,6 +57,21 @@ public class ConnectionManager {
closeConnection(connection);
continue;
}
if (connection.isNeedActiveTest()) {
boolean isActive = false;
try {
isActive = connection.activeTest();
} catch (IOException e) {
System.err.println("send to server[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] active test error ,emsg:" + e.getMessage());
isActive = false;
}
if (!isActive) {
closeConnection(connection);
continue;
} else {
connection.setNeedActiveTest(false);
}
}
} else if (ClientGlobal.g_connection_pool_max_count_per_entry == 0 || totalCount.get() < ClientGlobal.g_connection_pool_max_count_per_entry) {
connection = ConnectionFactory.create(this.inetSocketAddress);
totalCount.incrementAndGet();
@ -107,6 +122,20 @@ public class ConnectionManager {
}
}
public void setActiveTestFlag() {
if (freeCount.get() > 0) {
lock.lock();
try {
for (Connection freeConnection : freeConnections) {
freeConnection.setNeedActiveTest(true);
}
} finally {
lock.unlock();
}
}
}
@Override
public String toString() {
return "ConnectionManager{" +

View File

@ -54,6 +54,7 @@ public class ConnectionPool {
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.closeConnection(connection);
connectionManager.setActiveTestFlag();
} else {
connection.closeDirectly();
}