From 7896a007d3eabc2b17810c905022a89e6c70f06a Mon Sep 17 00:00:00 2001 From: Ghost_chu <30802565+Ghost-chu@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:03:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=20Banwave=20=E7=9A=84=E6=9C=AB?= =?UTF-8?q?=E5=B0=BE=E8=BF=9B=E8=A1=8C=E6=98=BE=E5=BC=8F=20GC=EF=BC=8C?= =?UTF-8?q?=E8=A6=81=E6=B1=82=20NativeImage=20=E9=87=8A=E6=94=BE=E5=86=85?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 + .../peerbanhelper/PeerBanHelperServer.java | 78 ++++++++++--------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/pom.xml b/pom.xml index 840bc2ba0c..03d840d079 100644 --- a/pom.xml +++ b/pom.xml @@ -211,6 +211,7 @@ --report-unsupported-elements-at-runtime --allow-incomplete-classpath --enable-url-protocols=http,https + --gc=serial -H:EnableURLProtocols=http -H:EnableURLProtocols=https -H:+ReportExceptionStackTraces @@ -274,6 +275,7 @@ -H:EnableURLProtocols=http -H:EnableURLProtocols=https -H:+ReportExceptionStackTraces + --gc=serial -H:ConfigurationFileDirectories=./src/main/resources/META-INF/native-image --initialize-at-build-time=org.slf4j.helpers.NOPLoggerFactory --initialize-at-build-time=org.slf4j.helpers.NOP_FallbackServiceProvider diff --git a/src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java b/src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java index dc4572219a..1804d1b22c 100644 --- a/src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java +++ b/src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java @@ -85,52 +85,56 @@ public void run() { } public void banWave() { - boolean needUpdate = false; - Set needRelaunched = new HashSet<>(downloaders.size()); - // 多线程处理下载器封禁操作 - for (Downloader downloader : downloaders) { - try { - if (!downloader.login()) { - log.warn(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint()); - return; - } - Pair> banDownloader = banDownloader(downloader); - if (banDownloader.getKey()) { - needUpdate = true; - } - needRelaunched.addAll(banDownloader.getValue()); - } catch (Throwable th) { - log.warn(Lang.ERR_UNEXPECTED_API_ERROR, downloader.getName(), downloader.getEndpoint(), th); - th.printStackTrace(); - } - } - - List removeBan = new ArrayList<>(); - for (Map.Entry pair : BAN_LIST.entrySet()) { - if (System.currentTimeMillis() >= pair.getValue().getUnbanAt()) { - removeBan.add(pair.getKey()); - } - } - - removeBan.forEach(this::unbanPeer); - if (!removeBan.isEmpty()) { - log.info(Lang.PEER_UNBAN_WAVE, removeBan.size()); - needUpdate = true; - } - - if (needUpdate) { + try { + boolean needUpdate = false; + Set needRelaunched = new HashSet<>(downloaders.size()); + // 多线程处理下载器封禁操作 for (Downloader downloader : downloaders) { try { if (!downloader.login()) { log.warn(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint()); return; } - downloader.setBanList(BAN_LIST.keySet()); - downloader.relaunchTorrentIfNeeded(needRelaunched); + Pair> banDownloader = banDownloader(downloader); + if (banDownloader.getKey()) { + needUpdate = true; + } + needRelaunched.addAll(banDownloader.getValue()); } catch (Throwable th) { - log.warn(Lang.ERR_UPDATE_BAN_LIST, downloader.getName(), downloader.getEndpoint(), th); + log.warn(Lang.ERR_UNEXPECTED_API_ERROR, downloader.getName(), downloader.getEndpoint(), th); + th.printStackTrace(); + } + } + + List removeBan = new ArrayList<>(); + for (Map.Entry pair : BAN_LIST.entrySet()) { + if (System.currentTimeMillis() >= pair.getValue().getUnbanAt()) { + removeBan.add(pair.getKey()); + } + } + + removeBan.forEach(this::unbanPeer); + if (!removeBan.isEmpty()) { + log.info(Lang.PEER_UNBAN_WAVE, removeBan.size()); + needUpdate = true; + } + + if (needUpdate) { + for (Downloader downloader : downloaders) { + try { + if (!downloader.login()) { + log.warn(Lang.ERR_CLIENT_LOGIN_FAILURE_SKIP, downloader.getName(), downloader.getEndpoint()); + return; + } + downloader.setBanList(BAN_LIST.keySet()); + downloader.relaunchTorrentIfNeeded(needRelaunched); + } catch (Throwable th) { + log.warn(Lang.ERR_UPDATE_BAN_LIST, downloader.getName(), downloader.getEndpoint(), th); + } } } + }finally { + System.gc(); // Trigger serial GC on GraalVM NativeImage to avoid took too much memory, we build NativeImage because it took less memory than JVM and faster startup speed } }