I want to check the backends' healthy conditon, if a backend is dead, i will deactive this backend.
I use a ThreadPool to start a thread, ping my backend using the ip + port, per 10s.
By using Socket class, I don't close the socket at first.
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress(domain, port);
try {
socket.connect(address, 3000);
if (socket.isConnected()) {
successCount++;
}
} catch (IOException e) {
e.printStackTrace();
}
Then I close the socket.
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress(domain, port);
try {
socket.connect(address, 3000);
if (socket.isConnected()) {
successCount++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I want to find the diffence between this conditon in usage of memory.
I use the jconsole to display the usage of memory.
But I find if i close the socket, the usage of memory is higher than i don't close it.
This is the picture about the memory. I want to know why about this. enter image description here
private void backendHealthCheck() {
Map<String, Boolean> backendHealthMap = new ConcurrentHashMap<>();
List<ProxyBackendConfiguration> allBackends = gatewayBackendManager.getAllBackends();
for (ProxyBackendConfiguration backend : allBackends) {
String backendName = backend.getName();
Boolean backendStatus = gatewayBackendManager.getBackendStatus(backendName);
log.debug("[{}] 对应的后端状态为 [{}]", backendName, backendStatus);
backendHealthMap.put(backendName, backendStatus);
}
scheduledExecutorService.scheduleWithFixedDelay(
() -> {
log.info("Check the backend's healthy condition");
List<ProxyBackendConfiguration> backends = gatewayBackendManager.getAllBackends();
for (ProxyBackendConfiguration backend : backends) {
int successCount = 0;
String domain = backend.getProxyTo().substring(8, 36);
int port = Integer.parseInt(backend.getProxyTo().substring(37, 41));
String backendName = backend.getName();
for (int i = 0; i < 7; i++) {
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress(domain, port);
try {
socket.connect(address, 3000);
if (socket.isConnected()) {
successCount++;
}
} catch (IOException e) {
e.printStackTrace();
}
if (successCount >= 2) {
// if the backend is active then break, else active this backend
// if this is not in map, get false active this backend
// if this backend in this map,and status is false,then active this backend
if (!backendHealthMap.getOrDefault(backendName, false)) {
gatewayBackendManager.activateBackend(backendName);
backendHealthMap.put(backendName, true);
}
break;
}
if (i == 6) {
// if this backend in map and the status is false, continue
// if this backend is not in map, get status is true,then deactive this backend
// if this backedn in map, and status is true,then deactive this backend
if (backendHealthMap.getOrDefault(backendName, true)) {
gatewayBackendManager.deactivateBackend(backendName);
backendHealthMap.put(backendName, false);
}
}
}
}
},
0,
10,
TimeUnit.SECONDS);
}