Skip to content

Commit

Permalink
在 Banwave 的末尾进行显式 GC,要求 NativeImage 释放内存
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Mar 27, 2024
1 parent 36672bc commit 7896a00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<buildArg>--report-unsupported-elements-at-runtime</buildArg>
<buildArg>--allow-incomplete-classpath</buildArg>
<buildArg>--enable-url-protocols=http,https</buildArg>
<buildArg>--gc=serial</buildArg>
<buildArg>-H:EnableURLProtocols=http</buildArg>
<buildArg>-H:EnableURLProtocols=https</buildArg>
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
Expand Down Expand Up @@ -274,6 +275,7 @@
<buildArg>-H:EnableURLProtocols=http</buildArg>
<buildArg>-H:EnableURLProtocols=https</buildArg>
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
<buildArg>--gc=serial</buildArg>
<buildArg>-H:ConfigurationFileDirectories=./src/main/resources/META-INF/native-image</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.helpers.NOPLoggerFactory</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.helpers.NOP_FallbackServiceProvider</buildArg>
Expand Down
78 changes: 41 additions & 37 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,52 +85,56 @@ public void run() {
}

public void banWave() {
boolean needUpdate = false;
Set<Torrent> 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<Boolean, Collection<Torrent>> 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<PeerAddress> removeBan = new ArrayList<>();
for (Map.Entry<PeerAddress, BanMetadata> 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<Torrent> 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<Boolean, Collection<Torrent>> 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<PeerAddress> removeBan = new ArrayList<>();
for (Map.Entry<PeerAddress, BanMetadata> 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
}
}

Expand Down

0 comments on commit 7896a00

Please sign in to comment.