Skip to content

Commit

Permalink
Merge pull request #275 from PBH-BTN/master
Browse files Browse the repository at this point in the history
v5.0.6
  • Loading branch information
Ghost-chu authored Jul 21, 2024
2 parents 49eaf4b + f6e3f80 commit 03a2b61
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.ghostchu.peerbanhelper</groupId>
<artifactId>peerbanhelper</artifactId>
<version>5.0.5</version>
<version>5.0.6</version>
<packaging>takari-jar</packaging>

<name>PeerBanHelper</name>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Slf4j
@Getter
public class BtnNetwork {
private static final int BTN_PROTOCOL_VERSION = 5;
private static final int BTN_PROTOCOL_VERSION = 6;
@Getter
private final Map<Class<? extends BtnAbility>, BtnAbility> abilities = new HashMap<>();
@Getter
Expand All @@ -55,7 +55,7 @@ public BtnNetwork(PeerBanHelperServer server, String userAgent, String configUrl
this.appId = appId;
this.appSecret = appSecret;
setupHttpClient();
executeService.scheduleAtFixedRate(this::checkIfNeedRetryConfig, 600, 600, TimeUnit.SECONDS);
executeService.scheduleWithFixedDelay(this::checkIfNeedRetryConfig, 600, 600, TimeUnit.SECONDS);
}

public void configBtnNetwork() {
Expand Down Expand Up @@ -98,11 +98,11 @@ public void configBtnNetwork() {
abilities.values().forEach(a -> {
try {
a.load();
configSuccess.set(true);
} catch (Exception e) {
log.error("Failed to load BTN ability", e);
}
});
configSuccess.set(true);
} catch (Throwable e) {
log.error(tlUI(Lang.BTN_CONFIG_FAILS, 600), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BtnAbilityReconfigure(BtnNetwork btnNetwork, JsonObject ability) {

@Override
public void load() {
btnNetwork.getExecuteService().scheduleAtFixedRate(this::checkIfReconfigure, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
btnNetwork.getExecuteService().scheduleWithFixedDelay(this::checkIfReconfigure, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
}

private void checkIfReconfigure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void load() {
} catch (Exception e) {
log.error("Unable to load cached BTN rules into memory");
}
btnNetwork.getExecuteService().scheduleAtFixedRate(this::updateRule, new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
btnNetwork.getExecuteService().scheduleWithFixedDelay(this::updateRule, new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
}

private void updateRule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
import com.ghostchu.peerbanhelper.btn.ping.BtnBan;
import com.ghostchu.peerbanhelper.btn.ping.BtnBanPing;
import com.ghostchu.peerbanhelper.btn.ping.BtnPeer;
import com.ghostchu.peerbanhelper.event.PeerBanEvent;
import com.ghostchu.peerbanhelper.module.impl.rule.BtnNetworkOnline;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.JsonUtil;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.github.mizosoft.methanol.MutableRequest;
import com.google.common.eventbus.Subscribe;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;

import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.ghostchu.peerbanhelper.text.TextManager.tl;
Expand All @@ -34,7 +28,6 @@ public class BtnAbilitySubmitBans implements BtnAbility {
private final long interval;
private final String endpoint;
private final long randomInitialDelay;
private final Map<BtnPeer, BanMetadata> bans = new ConcurrentHashMap<>();
private long lastReport = System.currentTimeMillis();

public BtnAbilitySubmitBans(BtnNetwork btnNetwork, JsonObject ability) {
Expand All @@ -47,18 +40,14 @@ public BtnAbilitySubmitBans(BtnNetwork btnNetwork, JsonObject ability) {
@Override
public void load() {
Main.getEventBus().register(this);
btnNetwork.getExecuteService().scheduleAtFixedRate(this::submit, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
btnNetwork.getExecuteService().scheduleWithFixedDelay(this::submit, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
}

@Override
public void unload() {
Main.getEventBus().unregister(this);
}

@Subscribe
public void onPeerBanEvent(PeerBanEvent event) {
bans.put(BtnPeer.from(event.getTorrentObj(), event.getPeerObj()), event.getBanMetadata());
}

private void submit() {
log.info(tlUI(Lang.BTN_SUBMITTING_BANS));
Expand All @@ -77,7 +66,6 @@ private void submit() {
} else {
log.info(tlUI(Lang.BTN_SUBMITTED_BANS, btnPeers.size()));
lastReport = System.currentTimeMillis();
bans.clear();
}
})
.exceptionally(e -> {
Expand All @@ -88,7 +76,9 @@ private void submit() {

private List<BtnBan> generateBans() {
List<BtnBan> list = new ArrayList<>();
for (Map.Entry<BtnPeer, BanMetadata> e : bans.entrySet()) {
Map<BtnPeer, BanMetadata> map = new HashMap<>();
btnNetwork.getServer().getBannedPeers().forEach((pa, meta) -> map.put(BtnPeer.from(meta.getTorrent(), meta.getPeer()), meta));
for (Map.Entry<BtnPeer, BanMetadata> e : map.entrySet()) {
if (e.getValue().getBanAt() <= lastReport) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public BtnAbilitySubmitPeers(BtnNetwork btnNetwork, JsonObject ability) {

@Override
public void load() {
btnNetwork.getExecuteService().scheduleAtFixedRate(this::submit, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
btnNetwork.getExecuteService().scheduleWithFixedDelay(this::submit, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
}

private void submit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BtnAbilitySubmitRulesHitRate(BtnNetwork btnNetwork, JsonObject ability) {

@Override
public void load() {
btnNetwork.getExecuteService().scheduleAtFixedRate(this::submit, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
btnNetwork.getExecuteService().scheduleWithFixedDelay(this::submit, interval + new Random().nextLong(randomInitialDelay), interval, TimeUnit.MILLISECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void onEnable() {
// 读取检查间隔
checkInterval = config.getLong("check-interval", checkInterval);
scheduledExecutorService = Executors.newScheduledThreadPool(1, Thread.ofVirtual().factory());
scheduledExecutorService.scheduleAtFixedRate(this::reloadConfig, 0, checkInterval, TimeUnit.MILLISECONDS);
scheduledExecutorService.scheduleWithFixedDelay(this::reloadConfig, 0, checkInterval, TimeUnit.MILLISECONDS);
}

@Override
Expand Down Expand Up @@ -384,7 +384,7 @@ public void changeCheckInterval(long checkInterval) throws IOException {
scheduledExecutorService.shutdown();
}
scheduledExecutorService = Executors.newScheduledThreadPool(1, r -> Thread.ofVirtual().name("IPBlackRuleList - Update Thread").unstarted(r));
scheduledExecutorService.scheduleAtFixedRate(this::reloadConfig, 0, checkInterval, TimeUnit.MILLISECONDS);
scheduledExecutorService.scheduleWithFixedDelay(this::reloadConfig, 0, checkInterval, TimeUnit.MILLISECONDS);
}

record IPBanResult(String ruleName, MatchResult matchResult) {
Expand Down

0 comments on commit 03a2b61

Please sign in to comment.