fix: NetworkInterfaceChecker can't worker

This commit is contained in:
tjq 2024-08-11 18:22:48 +08:00
parent fea1974014
commit a35573544c
5 changed files with 47 additions and 31 deletions

View File

@ -21,6 +21,7 @@ public class HttpUtils {
client = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
}

View File

@ -217,9 +217,10 @@ public class NetUtils {
if (networkInterfaceChecker == null) {
return false;
}
log.info("[Net] try to choose NetworkInterface by NetworkInterfaceChecker, current NetworkInterface: {}", networkInterface);
try {
return networkInterfaceChecker.ok(networkInterface, getFirstReachableInetAddress(networkInterface));
boolean ok = networkInterfaceChecker.ok(networkInterface, getFirstReachableInetAddress(networkInterface));
log.info("[Net] try to choose NetworkInterface by NetworkInterfaceChecker, current NetworkInterface[{}], ok: {}", networkInterface, ok);
return ok;
} catch (Exception e) {
log.warn("[Net] isPassedCheckerNetworkInterface failed, current networkInterface: {}", networkInterface, e);
}

View File

@ -35,7 +35,13 @@ public class PingPongSocketServer implements PingPongServer {
}
// 接收连接如果没有连接accept() 方法会阻塞
try (Socket socket = serverSocket.accept();OutputStream outputStream = socket.getOutputStream();) {
socket.setSoTimeout(2000);
socket.setKeepAlive(false);
outputStream.write(PingPongUtils.PONG.getBytes(StandardCharsets.UTF_8));
// BufferedReader.readLine() 会等待直到遇到换行符\n或回车符\r\n才会返回一行内容如果服务器发送的数据没有这些换行符readLine() 会一直阻塞直到超时
outputStream.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
outputStream.flush();
} catch (Exception e) {
if (!terminated) {

View File

@ -30,6 +30,9 @@ public class PingPongUtils {
try (Socket s = new Socket(targetIp, targetPort);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
s.setSoTimeout(2000);
s.setKeepAlive(false);
// 发送 PING 请求
os.write(PING.getBytes(StandardCharsets.UTF_8));
os.flush();

View File

@ -35,42 +35,47 @@ public class WorkerNetUtils {
pingPongServer = new PingPongSocketServer();
pingPongServer.initialize(port);
log.info("[WorkerNetUtils] initialize PingPongSocketServer successfully~");
return NetUtils.getLocalHostWithNetworkInterfaceChecker(((networkInterface, inetAddress) -> {
if (inetAddress == null) {
log.info("[WorkerNetUtils] [networkInterface:{}] skip due to inetAddress is null!", networkInterface);
return false;
}
String workerIp = inetAddress.getHostAddress();
for (String address : serverAddress) {
String url = String.format(SERVER_CONNECTIVITY_CHECK_URL_PATTERN, address, workerIp, port);
try {
String resp = HttpUtils.get(url);
log.info("[WorkerNetUtils] [networkInterface:{},inetAddress:{}] check connectivity by url[{}], response: {}", networkInterface, inetAddress, url, resp);
if (StringUtils.isNotEmpty(resp)) {
ResultDTO<?> resultDTO = JsonUtils.parseObject(resp, ResultDTO.class);
boolean ret = Boolean.TRUE.toString().equalsIgnoreCase(String.valueOf(resultDTO.getData()));
if (ret) {
return true;
}
}
} catch (Exception ignore) {
}
}
return false;
}));
} catch (Exception e) {
log.warn("[WorkerNetUtils] PingPongSocketServer failed to start, which may result in an incorrectly bound IP, please pay attention to the initialize log.", e);
}
String localHostWithNetworkInterfaceChecker = NetUtils.getLocalHostWithNetworkInterfaceChecker(((networkInterface, inetAddress) -> {
if (inetAddress == null) {
return false;
}
String workerIp = inetAddress.getHostAddress();
for (String address : serverAddress) {
String url = String.format(SERVER_CONNECTIVITY_CHECK_URL_PATTERN, address, workerIp, port);
} finally {
if (pingPongServer != null) {
try {
String resp = HttpUtils.get(url);
log.info("[WorkerNetUtils] check connectivity by url[{}], response: {}", url, resp);
if (StringUtils.isNotEmpty(resp)) {
ResultDTO<?> resultDTO = JsonUtils.parseObject(resp, ResultDTO.class);
return Boolean.TRUE.toString().equalsIgnoreCase(String.valueOf(resultDTO.getData()));
}
} catch (Exception ignore) {
pingPongServer.close();
log.info("[WorkerNetUtils] close PingPongSocketServer successfully~");
} catch (Exception e) {
log.warn("[WorkerNetUtils] close PingPongSocketServer failed!", e);
}
}
return false;
}));
if (pingPongServer != null) {
try {
pingPongServer.close();
log.info("[WorkerNetUtils] close PingPongSocketServer successfully~");
} catch (Exception e) {
log.warn("[WorkerNetUtils] close PingPongSocketServer failed!", e);
}
}
return localHostWithNetworkInterfaceChecker;
return NetUtils.getLocalHostWithNetworkInterfaceChecker(null);
}
}