mirror of
https://gitee.com/BDWare/agent-backend
synced 2025-01-10 01:44:14 +00:00
feat: adds auto-cleaning idle channels in the server
This commit is contained in:
parent
540a4232d0
commit
d4f1a9088b
@ -17,6 +17,8 @@ import io.netty.handler.ssl.OptionalSslHandler;
|
|||||||
import io.netty.handler.ssl.SslContext;
|
import io.netty.handler.ssl.SslContext;
|
||||||
import io.netty.handler.ssl.SslContextBuilder;
|
import io.netty.handler.ssl.SslContextBuilder;
|
||||||
import io.netty.handler.stream.ChunkedWriteHandler;
|
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.netty.util.internal.StringUtil;
|
||||||
import io.prometheus.client.exporter.HTTPServer;
|
import io.prometheus.client.exporter.HTTPServer;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
@ -95,12 +97,12 @@ public class CMHttpServer {
|
|||||||
if (!cmdConf.withBdledgerClient.isEmpty()) {
|
if (!cmdConf.withBdledgerClient.isEmpty()) {
|
||||||
ContractManager.scheduledThreadPool.schedule(() -> {
|
ContractManager.scheduledThreadPool.schedule(() -> {
|
||||||
File ledgerClient = new File(cmdConf.withBdledgerClient);
|
File ledgerClient = new File(cmdConf.withBdledgerClient);
|
||||||
LOGGER.debug("canRead=" + ledgerClient.canRead() + " path="
|
LOGGER.debug("canRead={} path={}", ledgerClient.canRead(),
|
||||||
+ ledgerClient.getAbsolutePath());
|
ledgerClient.getAbsolutePath());
|
||||||
try {
|
try {
|
||||||
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
|
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.warn("start bdledger client failed: " + e.getMessage());
|
LOGGER.warn("start bdledger client failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}, 1, TimeUnit.SECONDS);
|
}, 1, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
@ -118,10 +120,10 @@ public class CMHttpServer {
|
|||||||
.stream().filter(x -> null != x && !x.contains("RC4"))
|
.stream().filter(x -> null != x && !x.contains("RC4"))
|
||||||
.toArray(String[]::new))
|
.toArray(String[]::new))
|
||||||
.build();
|
.build();
|
||||||
LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable());
|
LOGGER.info("openssl isAvailable:{}", OpenSsl.isAvailable());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.warn("Enabling SSL failed: " + e.getMessage());
|
LOGGER.warn("Enabling SSL failed: {}", e.getMessage());
|
||||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,7 +133,7 @@ public class CMHttpServer {
|
|||||||
FileWriter fw = new FileWriter("CMI");
|
FileWriter fw = new FileWriter("CMI");
|
||||||
fw.write(ContractClient.cmi);
|
fw.write(ContractClient.cmi);
|
||||||
fw.close();
|
fw.close();
|
||||||
LOGGER.info("write CMI=" + ContractClient.cmi + " to ./CMI");
|
LOGGER.info("write CMI={} to ./CMI", ContractClient.cmi);
|
||||||
FileActions.setTextFileSuffixes(cmdConf.textFileSuffixes);
|
FileActions.setTextFileSuffixes(cmdConf.textFileSuffixes);
|
||||||
|
|
||||||
// plugins
|
// plugins
|
||||||
@ -149,7 +151,7 @@ public class CMHttpServer {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Configurator.setLevel(clz, Level.DEBUG);
|
Configurator.setLevel(clz, Level.DEBUG);
|
||||||
LOGGER.warn("set debug: " + clz);
|
LOGGER.warn("set debug: {}", clz);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.warn(e.getMessage());
|
LOGGER.warn(e.getMessage());
|
||||||
@ -190,7 +192,7 @@ public class CMHttpServer {
|
|||||||
|
|
||||||
private static void addDirToPath(String s) {
|
private static void addDirToPath(String s) {
|
||||||
try {
|
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("sys_paths");
|
||||||
Field field = ClassLoader.class.getDeclaredField("usr_paths");
|
Field field = ClassLoader.class.getDeclaredField("usr_paths");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
@ -262,8 +264,8 @@ public class CMHttpServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void start(int port) throws IOException {
|
public static void start(int port) throws IOException {
|
||||||
LOGGER.info("start server at:" + port);
|
LOGGER.info("start server at:{}", port);
|
||||||
LOGGER.debug("dir:" + new File("./").getAbsolutePath());
|
LOGGER.debug("dir:{}", new File("./").getAbsolutePath());
|
||||||
new CMHttpServer(port).start();
|
new CMHttpServer(port).start();
|
||||||
// never reach here!!
|
// never reach here!!
|
||||||
}
|
}
|
||||||
@ -285,7 +287,7 @@ public class CMHttpServer {
|
|||||||
for (int i = 0; i < pluginJar.length; i++) {
|
for (int i = 0; i < pluginJar.length; i++) {
|
||||||
try {
|
try {
|
||||||
urls[i] = pluginJar[i].toURI().toURL();
|
urls[i] = pluginJar[i].toURI().toURL();
|
||||||
LOGGER.info("add plugin:" + pluginJar[i].getName());
|
LOGGER.info("add plugin:{}", pluginJar[i].getName());
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -319,10 +321,24 @@ public class CMHttpServer {
|
|||||||
.addLast(new WebSocketServerProtocolHandler(PATH, null, true))
|
.addLast(new WebSocketServerProtocolHandler(PATH, null, true))
|
||||||
.addLast(new ChunkedWriteHandler()).addLast(serverHandler)
|
.addLast(new ChunkedWriteHandler()).addLast(serverHandler)
|
||||||
.addLast(new ContractManagerFrameHandler());
|
.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();
|
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);
|
new HTTPServer(port + 3);
|
||||||
NetworkManager.instance.initTCP(port + 1, workerGroup);
|
NetworkManager.instance.initTCP(port + 1, workerGroup);
|
||||||
|
|
||||||
@ -340,7 +356,7 @@ public class CMHttpServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadStartContractConfiguration() {
|
private void loadStartContractConfiguration() {
|
||||||
if (cmdConf.startContract != null && cmdConf.startContract.size() > 0) {
|
if (cmdConf.startContract != null && !cmdConf.startContract.isEmpty()) {
|
||||||
ContractManager.scheduledThreadPool.schedule(() -> {
|
ContractManager.scheduledThreadPool.schedule(() -> {
|
||||||
for (JsonElement je : cmdConf.startContract) {
|
for (JsonElement je : cmdConf.startContract) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user