Skip to content

Commit

Permalink
优化 BtnRuleParsed 在解析过程中的内存尖峰问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Dec 5, 2024
1 parent 0628749 commit 4c3214b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.ghostchu.peerbanhelper.util.rule.Rule;
import com.ghostchu.peerbanhelper.util.rule.RuleParser;
import com.ghostchu.peerbanhelper.util.rule.matcher.IPMatcher;
import inet.ipaddr.IPAddress;
import inet.ipaddr.format.util.DualIPv4v6Tries;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -94,10 +94,13 @@ public String matcherIdentifier() {
return rules;
}


public Map<String, IPMatcher> parseIPRule(Map<String, List<String>> raw) {
Map<String, IPMatcher> rules = new HashMap<>();
raw.forEach((k, v) -> rules.put(k,new IPMatcher(version, k, v.stream().map(IPAddressUtil::getIPAddress).toList())));
raw.forEach((k, v) -> {
DualIPv4v6Tries tries = new DualIPv4v6Tries();
v.stream().map(IPAddressUtil::getIPAddress).forEach(tries::add);
rules.put(k,new IPMatcher(version, k, List.of(tries)));
});
return rules;
}

Expand All @@ -120,7 +123,7 @@ public static class BtnRuleIpMatcher extends IPMatcher {

private final String version;

public BtnRuleIpMatcher(String version, String ruleId, String ruleName, List<IPAddress> ruleData) {
public BtnRuleIpMatcher(String version, String ruleId, String ruleName, List<DualIPv4v6Tries> ruleData) {
super(ruleId, ruleName, ruleData);
this.version = version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.io.Files;
import com.j256.ormlite.stmt.SelectArg;
import inet.ipaddr.IPAddress;
import inet.ipaddr.format.util.DualIPv4v6Tries;
import io.ipfs.cid.Cid;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -202,7 +203,7 @@ public StdResp updateRule(String locale, @NotNull ConfigurationSection rule, IPB
File dir = new File(Main.getDataDirectory(), "/sub");
dir.mkdirs();
File ruleFile = new File(dir, ruleFileName);
List<IPAddress> ipAddresses = new ArrayList<>();
DualIPv4v6Tries ipAddresses = new DualIPv4v6Tries();
getResource(url)
.whenComplete((dataUpdateResult, throwable) -> {
if (throwable != null) {
Expand All @@ -212,7 +213,7 @@ public StdResp updateRule(String locale, @NotNull ConfigurationSection rule, IPB
if (ipBanMatchers.stream().noneMatch(ele -> ele.getRuleId().equals(ruleId))) {
try {
fileToIPList(ruleFile, ipAddresses);
ipBanMatchers.add(new IPMatcher(ruleId, name, ipAddresses));
ipBanMatchers.add(new IPMatcher(ruleId, name, List.of(ipAddresses)));
log.warn(tlUI(Lang.IP_BAN_RULE_USE_CACHE, name));
result.set(new StdResp(false, tl(locale, Lang.IP_BAN_RULE_USE_CACHE, name), null));
} catch (IOException ex) {
Expand Down Expand Up @@ -252,12 +253,12 @@ public StdResp updateRule(String locale, @NotNull ConfigurationSection rule, IPB
if (ent_count > 0) {
// 如果已经存在则更新,否则添加
ipBanMatchers.stream().filter(ele -> ele.getRuleId().equals(ruleId)).findFirst().ifPresentOrElse(ele -> {
ele.setData(name, ipAddresses);
ele.setData(name, List.of(ipAddresses));
moduleMatchCache.invalidateAll();
log.info(tlUI(Lang.IP_BAN_RULE_UPDATE_SUCCESS, name));
result.set(new StdResp(true, tl(locale, Lang.IP_BAN_RULE_UPDATE_SUCCESS, name), null));
}, () -> {
ipBanMatchers.add(new IPMatcher(ruleId, name, ipAddresses));
ipBanMatchers.add(new IPMatcher(ruleId, name, List.of(ipAddresses)));
log.info(tlUI(Lang.IP_BAN_RULE_LOAD_SUCCESS, name));
result.set(new StdResp(true, tl(locale, Lang.IP_BAN_RULE_LOAD_SUCCESS, name), null));
});
Expand Down Expand Up @@ -338,7 +339,7 @@ private CompletableFuture<DataUpdateResult> getResource(String url) {
* @param ips ip列表
* @return 加载的行数
*/
private int fileToIPList(File ruleFile, List<IPAddress> ips) throws IOException {
private int fileToIPList(File ruleFile, DualIPv4v6Tries ips) throws IOException {
AtomicInteger count = new AtomicInteger();
Files.readLines(ruleFile, StandardCharsets.UTF_8).stream().filter(s -> !s.isBlank()).forEach(ele -> {
if (ele.startsWith("#")) {
Expand All @@ -364,7 +365,7 @@ private int fileToIPList(File ruleFile, List<IPAddress> ips) throws IOException
* @param ips ip列表
* @return 加载的行数
*/
private int stringToIPList(String data, List<IPAddress> ips) throws IOException {
private int stringToIPList(String data, DualIPv4v6Tries ips) throws IOException {
AtomicInteger count = new AtomicInteger();
Arrays.stream(data.split("\n")).filter(s -> !s.isBlank()).forEach(ele -> {
if (ele.startsWith("#")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
@Slf4j
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class IPMatcher extends RuleMatcher<IPAddress> {
private DualIPv4v6Tries ips = new DualIPv4v6Tries();
public class IPMatcher extends RuleMatcher<DualIPv4v6Tries> {
private DualIPv4v6Tries ips;

public IPMatcher(String ruleId, String ruleName, List<IPAddress> ruleData) {
public IPMatcher(String ruleId, String ruleName, List<DualIPv4v6Tries> ruleData) {
super(ruleId, ruleName, ruleData);
ruleData.forEach(ips::add);
this.ips = ruleData.getFirst();;
}

/**
Expand All @@ -31,11 +31,9 @@ public IPMatcher(String ruleId, String ruleName, List<IPAddress> ruleData) {
* @param ruleName 规则名
* @param ruleData 规则数据
*/
public void setData(String ruleName, List<IPAddress> ruleData) {
public void setData(String ruleName, List<DualIPv4v6Tries> ruleData) {
setRuleName(ruleName);
var tmp = new DualIPv4v6Tries();
ruleData.forEach(tmp::add);
this.ips = tmp;
this.ips = ruleData.getFirst();
}

public long size() {
Expand Down

0 comments on commit 4c3214b

Please sign in to comment.