feat: adds auto-cleaning idle channels in the server

This commit is contained in:
Kaidong Wu 2024-04-24 14:13:39 +08:00
parent 540a4232d0
commit d4f1a9088b
Signed by untrusted user who does not match committer: wukaidong
GPG Key ID: B08F5A25D4B4BE65

View File

@ -17,6 +17,8 @@ import io.netty.handler.ssl.OptionalSslHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.internal.StringUtil;
import io.prometheus.client.exporter.HTTPServer;
import org.apache.commons.io.FileUtils;
@ -95,12 +97,12 @@ public class CMHttpServer {
if (!cmdConf.withBdledgerClient.isEmpty()) {
ContractManager.scheduledThreadPool.schedule(() -> {
File ledgerClient = new File(cmdConf.withBdledgerClient);
LOGGER.debug("canRead=" + ledgerClient.canRead() + " path="
+ ledgerClient.getAbsolutePath());
LOGGER.debug("canRead={} path={}", ledgerClient.canRead(),
ledgerClient.getAbsolutePath());
try {
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
} catch (IOException e) {
LOGGER.warn("start bdledger client failed: " + e.getMessage());
LOGGER.warn("start bdledger client failed: {}", e.getMessage());
}
}, 1, TimeUnit.SECONDS);
}
@ -118,10 +120,10 @@ public class CMHttpServer {
.stream().filter(x -> null != x && !x.contains("RC4"))
.toArray(String[]::new))
.build();
LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable());
LOGGER.info("openssl isAvailable:{}", OpenSsl.isAvailable());
}
} catch (Exception e) {
LOGGER.warn("Enabling SSL failed: " + e.getMessage());
LOGGER.warn("Enabling SSL failed: {}", e.getMessage());
LOGGER.debug(ExceptionUtil.exceptionToString(e));
}
}
@ -131,7 +133,7 @@ public class CMHttpServer {
FileWriter fw = new FileWriter("CMI");
fw.write(ContractClient.cmi);
fw.close();
LOGGER.info("write CMI=" + ContractClient.cmi + " to ./CMI");
LOGGER.info("write CMI={} to ./CMI", ContractClient.cmi);
FileActions.setTextFileSuffixes(cmdConf.textFileSuffixes);
// plugins
@ -149,7 +151,7 @@ public class CMHttpServer {
break;
}
Configurator.setLevel(clz, Level.DEBUG);
LOGGER.warn("set debug: " + clz);
LOGGER.warn("set debug: {}", clz);
}
} catch (Exception e) {
LOGGER.warn(e.getMessage());
@ -190,7 +192,7 @@ public class CMHttpServer {
private static void addDirToPath(String s) {
try {
LOGGER.info("add to path: " + s);
LOGGER.info("add to path: {}", s);
// Field field = ClassLoader.class.getDeclaredField("sys_paths");
Field field = ClassLoader.class.getDeclaredField("usr_paths");
field.setAccessible(true);
@ -262,8 +264,8 @@ public class CMHttpServer {
}
public static void start(int port) throws IOException {
LOGGER.info("start server at:" + port);
LOGGER.debug("dir:" + new File("./").getAbsolutePath());
LOGGER.info("start server at:{}", port);
LOGGER.debug("dir:{}", new File("./").getAbsolutePath());
new CMHttpServer(port).start();
// never reach here!!
}
@ -285,7 +287,7 @@ public class CMHttpServer {
for (int i = 0; i < pluginJar.length; i++) {
try {
urls[i] = pluginJar[i].toURI().toURL();
LOGGER.info("add plugin:" + pluginJar[i].getName());
LOGGER.info("add plugin:{}", pluginJar[i].getName());
} catch (MalformedURLException e) {
e.printStackTrace();
}
@ -319,10 +321,24 @@ public class CMHttpServer {
.addLast(new WebSocketServerProtocolHandler(PATH, null, true))
.addLast(new ChunkedWriteHandler()).addLast(serverHandler)
.addLast(new ContractManagerFrameHandler());
arg0.pipeline().addLast(new IdleStateHandler(120, 0, 0))
.addLast(new ChannelDuplexHandler() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx,
Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent stateEvent = (IdleStateEvent) evt;
LOGGER.warn(
"Detected channel idle event: {}; auto close!",
stateEvent.state());
ctx.channel().close();
}
}
});
}
});
final Channel ch = b1.bind(port).sync().channel();
LOGGER.debug("[CMHttpServer] listen master port at:" + port);
LOGGER.debug("[CMHttpServer] listen master port at:{}", port);
new HTTPServer(port + 3);
NetworkManager.instance.initTCP(port + 1, workerGroup);
@ -340,7 +356,7 @@ public class CMHttpServer {
}
private void loadStartContractConfiguration() {
if (cmdConf.startContract != null && cmdConf.startContract.size() > 0) {
if (cmdConf.startContract != null && !cmdConf.startContract.isEmpty()) {
ContractManager.scheduledThreadPool.schedule(() -> {
for (JsonElement je : cmdConf.startContract) {
try {