feature:add active test flag
parent
70599a1656
commit
f742f3c6aa
|
@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,6 +57,16 @@ 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) continue;
|
||||
}
|
||||
} 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 +117,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{" +
|
||||
|
|
|
@ -54,6 +54,7 @@ public class ConnectionPool {
|
|||
ConnectionManager connectionManager = CP.get(key);
|
||||
if (connectionManager != null) {
|
||||
connectionManager.closeConnection(connection);
|
||||
connectionManager.setActiveTestFlag();
|
||||
} else {
|
||||
connection.closeDirectly();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue