Merge branch 'feature_add_connect_pool_20191205'
commit
06f5624a8d
|
@ -15,6 +15,8 @@ public class Connection {
|
||||||
private InetSocketAddress inetSockAddr;
|
private InetSocketAddress inetSockAddr;
|
||||||
private Long lastAccessTime = System.currentTimeMillis();
|
private Long lastAccessTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
private boolean needActiveTest = false;
|
||||||
|
|
||||||
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
|
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
|
||||||
this.sock = sock;
|
this.sock = sock;
|
||||||
this.inetSockAddr = inetSockAddr;
|
this.inetSockAddr = inetSockAddr;
|
||||||
|
@ -117,11 +119,21 @@ public class Connection {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isNeedActiveTest() {
|
||||||
|
return needActiveTest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNeedActiveTest(boolean needActiveTest) {
|
||||||
|
this.needActiveTest = needActiveTest;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "TrackerServer{" +
|
return "Connection{" +
|
||||||
"sock=" + sock +
|
"sock=" + sock +
|
||||||
", inetSockAddr=" + inetSockAddr +
|
", inetSockAddr=" + inetSockAddr +
|
||||||
|
", lastAccessTime=" + lastAccessTime +
|
||||||
|
", needActiveTest=" + needActiveTest +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,21 @@ public class ConnectionManager {
|
||||||
closeConnection(connection);
|
closeConnection(connection);
|
||||||
continue;
|
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) {
|
} 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);
|
connection = ConnectionFactory.create(this.inetSocketAddress);
|
||||||
totalCount.incrementAndGet();
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ConnectionManager{" +
|
return "ConnectionManager{" +
|
||||||
|
|
|
@ -54,6 +54,7 @@ public class ConnectionPool {
|
||||||
ConnectionManager connectionManager = CP.get(key);
|
ConnectionManager connectionManager = CP.get(key);
|
||||||
if (connectionManager != null) {
|
if (connectionManager != null) {
|
||||||
connectionManager.closeConnection(connection);
|
connectionManager.closeConnection(connection);
|
||||||
|
connectionManager.setActiveTestFlag();
|
||||||
} else {
|
} else {
|
||||||
connection.closeDirectly();
|
connection.closeDirectly();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue