Skip to content

Commit

Permalink
稍微优化一点点性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 1, 2025
1 parent d7c8a34 commit 9fa1d92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,18 @@ public void cronUpdateTrunkerFile() {
((peerInfo.getUserAgent().contains("Transmission") == peerId.startsWith("-TR")))
|| ((peerInfo.getUserAgent().contains("aria2") == peerId.startsWith("A2")))
) {
ipTries.add(IPUtil.toIPAddress(peerInfo.getIp().getClientIp().toByteArray()));
var ip = IPUtil.toIPAddress(peerInfo.getIp().getClientIp().toByteArray());
if (!ip.isLocal() && !ip.isLoopback()) {
try {
if (ip.getPrefixLength() == null && ip.isIPv6()) {
ip = ip.toPrefixBlock(ipv6ConvertToPrefixLength);
}
ipTries.add(ip);
} catch (Exception e) {
log.error("Unable to convert {} with prefix block {}.", ip, ipv6ConvertToPrefixLength, e);
}
}

}
success.incrementAndGet();
} catch (Exception e) {
Expand All @@ -307,8 +318,10 @@ public void cronUpdateTrunkerFile() {
}, service);
}

var filtered = filterIP(ipTries);
var ips = ipMerger.merge(filtered).stream().distinct().map(ip -> new AnalysedRule(null, ip, TRACKER_HIGH_RISK, "Auto Generated by Sparkle")).collect(Collectors.toSet());
// var filtered = filterIP(ipTries); // too slow


var ips = ipMerger.merge(ipTries).stream().distinct().map(ip -> new AnalysedRule(null, ip, TRACKER_HIGH_RISK, "Auto Generated by Sparkle")).collect(Collectors.toSet());
analysedRuleRepository.replaceAll(TRACKER_HIGH_RISK, ips);
meterRegistry.gauge("sparkle_analyse_tracker_high_risk_identity", Collections.emptyList(), ips.size());
log.info("Tracker HighRisk identity: {}, tooked {} ms; success: {}/{}.", ips.size(), System.currentTimeMillis() - startAt, success.get(), count.get());
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/ghostchu/btn/sparkle/util/IPMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public List<String> merge(Collection<String> sorted) {
Set<String> createdCIDR = new TreeSet<>();
IPAddress current = null;
int counter = 0;
list.removeIf(str -> str.startsWith("#"));
for (String rule : list) {
if (rule.startsWith("#")) continue;
if (current == null) {
current = toCIDR(rule);
continue;
Expand Down Expand Up @@ -66,10 +66,9 @@ public List<String> merge(Collection<String> sorted) {
}
}
}
List<String> finalSorted = list;
createdCIDR.forEach(cidr -> {
var base = toCIDR(cidr);
finalSorted.removeIf(ip -> {
((List<String>) list).removeIf(ip -> {
IPAddress address = toIP(ip);
if (address == null) {
System.out.println("(Unresolved data) " + ip);
Expand All @@ -81,7 +80,7 @@ public List<String> merge(Collection<String> sorted) {
});
return new ArrayList<>() {{
this.addAll(createdCIDR);
this.addAll(finalSorted);
this.addAll(list);
}};
}

Expand Down

0 comments on commit 9fa1d92

Please sign in to comment.