Skip to content

Commit

Permalink
实现 PeerThrottling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Feb 1, 2025
1 parent fed7e19 commit 37c2103
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,6 @@ default void runScheduleTasks() {
List<DownloaderFeatureFlag> getFeatureFlags();

int getMaxConcurrentPeerRequestSlots();

void throttlePeer(Torrent torrent, Peer peer, long uploadRate, long downloadRate) throws UnsupportedOperationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.ConnectorData;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.bean.clientbound.BanBean;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.bean.clientbound.BanListReplacementBean;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.bean.clientbound.PeerThrottlingBean;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.bean.serverbound.MetadataCallbackBean;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.wrapper.DownloadRecord;
import com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.wrapper.PeerManagerRecord;
Expand All @@ -25,6 +26,7 @@
import com.ghostchu.peerbanhelper.torrent.TrackerImpl;
import com.ghostchu.peerbanhelper.util.ByteUtil;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.UrlEncoderDecoder;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.wrapper.BanMetadata;
import com.ghostchu.peerbanhelper.wrapper.PeerAddress;
Expand Down Expand Up @@ -97,7 +99,33 @@ public static BiglyBT loadFromConfig(String name, ConfigurationSection section,

@Override
public List<DownloaderFeatureFlag> getFeatureFlags() {
return List.of(DownloaderFeatureFlag.READ_PEER_PROTOCOLS, DownloaderFeatureFlag.UNBAN_IP, DownloaderFeatureFlag.THROTTLING);
List<DownloaderFeatureFlag> flags = new ArrayList<>();
flags.add(DownloaderFeatureFlag.READ_PEER_PROTOCOLS);
flags.add(DownloaderFeatureFlag.UNBAN_IP);
if (semver.isGreaterThanOrEqualTo("1.3.0")) {
flags.add(DownloaderFeatureFlag.THROTTLING);
}
return flags;
}

@Override
public void throttlePeer(Torrent torrent, Peer peer, long uploadRate, long downloadRate) throws UnsupportedOperationException {
if (!getFeatureFlags().contains(DownloaderFeatureFlag.THROTTLING)) {
throw new UnsupportedOperationException("BiglyBT-Adapter version is lower than 1.3.0, throttling is not supported.");
}
PeerThrottlingBean bean = new PeerThrottlingBean((int) uploadRate, (int) downloadRate);
HttpResponse<String> resp;
try {
var urlIp = UrlEncoderDecoder.encodeToLegalPath(peer.getRawIp());
resp = httpClient.send(MutableRequest.POST(apiEndpoint + "/download/" + torrent.getHash() + "/peer/" + urlIp + "/throttling",
HttpRequest.BodyPublishers.ofString(JsonUtil.getGson().toJson(bean), StandardCharsets.UTF_8)),
HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new IllegalStateException(e);
}
if (resp.statusCode() != 200) {
throw new IllegalStateException(tlUI(Lang.DOWNLOADER_BIGLYBT_THROTTLE_FAILED, getName(), resp.statusCode(), resp.body()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ghostchu.peerbanhelper.downloader.impl.biglybt.network.bean.clientbound;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class PeerThrottlingBean {
private int uploadRate;
private int downloadRate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ public int getMaxConcurrentPeerRequestSlots() {
return 4;
}

@Override
public void throttlePeer(Torrent torrent, Peer peer, long uploadRate, long downloadRate) throws UnsupportedOperationException {
throw new UnsupportedOperationException("BitComet does not support throttling peers");
}

@NoArgsConstructor
@Data
public static class Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public DownloaderStatistics getStatistics() {
return new DownloaderStatistics(0,0);
}

@Override
public void throttlePeer(Torrent torrent, Peer peer, long uploadRate, long downloadRate) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Deluge does not support throttling peers");
}

@Override
public void close() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ public List<Peer> getPeers(Torrent torrent) {
return peersList;
}

@Override
public void throttlePeer(Torrent torrent, Peer peer, long uploadRate, long downloadRate) throws UnsupportedOperationException {
throw new UnsupportedOperationException("qBittorrent does not support throttling peers");
}

protected void setBanListIncrement(Collection<BanMetadata> added) {
Map<String, StringJoiner> banTasks = new HashMap<>();
added.forEach(p -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public DownloaderStatistics getStatistics() {
return new DownloaderStatistics(stats.getCumulativeStats().getUploadedBytes(), stats.getCumulativeStats().getDownloadedBytes());
}

@Override
public void throttlePeer(Torrent torrent, Peer peer, long uploadRate, long downloadRate) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Transmission does not support throttling peer");
}

@Override
public void relaunchTorrentIfNeeded(Collection<Torrent> torrents) {
relaunchTorrents(torrents.stream()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ghostchu/peerbanhelper/text/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public enum Lang {
LAB_EXPERIMENT_FILL_MISSING_DATA_IN_TRAFFIC_SUMMARY_DESCRIPTION,
GUI_TABBED_WEBUI,
JCEF_BROWSER_UNSUPPORTED_PLATFORM,
JCEF_BROWSER_UNSUPPORTED_EXCEPTION, JCEF_DOWNLOAD_TITLE, JCEF_DOWNLOAD_DESCRIPTION, GUI_COMMON_CANCEL, JCEF_DOWNLOAD_UNZIP_DESCRIPTION, IPDB_DOWNLOAD_TITLE, IPDB_DOWNLOAD_DESCRIPTION, PBH_PLUS_THANKS_FOR_DONATION_GUI_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_DESCRIPTION, GUI_TITLE_DEBUG, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_TITLE, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION, ABOUT_VIEW_CREDIT, INCOMPATIBLE_BITNESS_TITLE, INCOMPATIBLE_BITNESS_DESCRIPTION, TITLE_INCOMPATIBLE_PLATFORM, INCOMPATIBLE_BITNESS_LOG, LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE, LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION, TITLE_APP_V_CONTAINER;
JCEF_BROWSER_UNSUPPORTED_EXCEPTION, JCEF_DOWNLOAD_TITLE, JCEF_DOWNLOAD_DESCRIPTION, GUI_COMMON_CANCEL, JCEF_DOWNLOAD_UNZIP_DESCRIPTION, IPDB_DOWNLOAD_TITLE, IPDB_DOWNLOAD_DESCRIPTION, PBH_PLUS_THANKS_FOR_DONATION_GUI_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_DESCRIPTION, GUI_TITLE_DEBUG, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_TITLE, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION, ABOUT_VIEW_CREDIT, INCOMPATIBLE_BITNESS_TITLE, INCOMPATIBLE_BITNESS_DESCRIPTION, TITLE_INCOMPATIBLE_PLATFORM, INCOMPATIBLE_BITNESS_LOG, LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE, LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION, TITLE_APP_V_CONTAINER, DOWNLOADER_BIGLYBT_THROTTLE_FAILED;

public String getKey() {
return name();
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/lang/en-us/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,5 @@ DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION: |
For assistance, please join the PeerBanHelper community. **This prompt will only appear once per downloader.**
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE: "Enable transaction batch write ban history"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION: "Use transactions to batch write to the database when recording ban history to improve performance and reduce random disk I/O operations."
TITLE_APP_V_CONTAINER: "📦App-V"
TITLE_APP_V_CONTAINER: "📦App-V"
DOWNLOADER_BIGLYBT_THROTTLE_FAILED: "Failed to set Peer throttle property for downloader {}, remote API call failed: {} - {}"
3 changes: 2 additions & 1 deletion src/main/resources/lang/messages_fallback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,5 @@ INCOMPATIBLE_BITNESS_LOG: "在 32 位操作系统/JVM 上运行 PeerBanHelper
TITLE_INCOMPATIBLE_PLATFORM: "⚠不受支持的平台"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE: "启用事务批量写入封禁历史记录"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION: "在记录封禁历史记录时使用事务以批量写入数据库以提高性能和减少随机磁盘 I/O 操作。"
TITLE_APP_V_CONTAINER: "📦App-V"
TITLE_APP_V_CONTAINER: "📦App-V"
DOWNLOADER_BIGLYBT_THROTTLE_FAILED: "无法设置下载器 {} 的 Peer 节流属性,远程 API 调用发生错误: {} - {}"
3 changes: 2 additions & 1 deletion src/main/resources/lang/zh-cn/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,5 @@ DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION: |
ABOUT_VIEW_CREDIT: "开发人员与社区贡献者..."
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE: "启用事务批量写入封禁历史记录"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION: "在记录封禁历史记录时使用事务以批量写入数据库以提高性能和减少随机磁盘 I/O 操作。"
TITLE_APP_V_CONTAINER: "📦App-V"
TITLE_APP_V_CONTAINER: "📦App-V"
DOWNLOADER_BIGLYBT_THROTTLE_FAILED: "无法设置下载器 {} 的 Peer 节流属性,远程 API 调用发生错误: {} - {}"

0 comments on commit 37c2103

Please sign in to comment.