Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: detect private torrents on qBittorrents v4.x #477

Merged
merged 21 commits into from
Sep 16, 2024

Conversation

ccloli
Copy link
Contributor

@ccloli ccloli commented Sep 15, 2024

为 qBittorrent 4.x 添加私有种子检测支持。

qBittorrent 在 5.x 上为 /api/v2/torrents/info API 返回了 is_private 字段,对于接口未返回该字段的情况(如 qBittorrent 4.x),现在会再调用 /api/v2/torrents/properties API 获取 torrent 的完整信息,得到 is_private 字段,从而在低版本 qBittorrent 上支持过滤私有 torrent。

为了减少请求开销,上述功能仅在需要时执行,最大并发 5 线程请求 qBittorrent 的 API 接口,且会将结果缓存,避免重复请求。

相关的粗略性能调试结果可参考 #472 (comment) ,本 PR 的测试用构建产物可在 这里 获取。

Fixes #472 .


Disclaimer: 本 PR 内的修改包含使用 ChatGPT 生成的源代码,若该行为不符合项目的贡献规范,可以关闭本 PR。

Summary by CodeRabbit

  • New Features

    • Introduced a caching mechanism for private torrent detection, enhancing performance and reducing API calls.
    • Added a configuration option to ignore private torrents, allowing users to customize their experience.
    • Enhanced the handling of private torrents with asynchronous processing and caching mechanisms.
    • Updated localization strings for "Ignore Private Torrents" to clarify performance implications on older qBittorrent versions.
  • Bug Fixes

    • Improved user guidance by modifying outdated descriptions regarding the "ignore private" feature in the user interface.

@ccloli ccloli requested review from a team as code owners September 15, 2024 10:44
Copy link
Contributor

coderabbitai bot commented Sep 15, 2024

Walkthrough

The changes introduced in this pull request enhance the handling of private torrents in the downloader by implementing caching mechanisms and asynchronous processing. A new cache is added to store the privacy status of torrents, reducing API calls. The retrieval of torrent details is optimized using an ExecutorService for concurrent processing, while a semaphore limits concurrent requests. Additionally, user interface elements related to the "ignore private" feature are updated to clarify compatibility with qBittorrent versions.

Changes

Files Change Summary
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrent.java
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java
Introduced a caching mechanism for private torrent detection, utilizing Cache<String, Boolean>, ExecutorService, and Semaphore. Modified getTorrents() to use asynchronous processing and added getPrivateStatus() method for privacy status checks.
webui/src/views/dashboard/locale/en-US.ts
webui/src/views/dashboard/locale/zh-CN.ts
Updated localization strings for the "Ignore Private Torrents" feature to clarify potential performance impacts on versions earlier than qBittorrent 5.x.

Assessment against linked issues

Objective Addressed Explanation
Support exclusion of private torrents in qBittorrent 4.x ( #472 )
Implement caching for private torrent checks ( #472 )

Possibly related PRs

Poem

🐇 In the garden where torrents flow,
A rabbit hops, with changes aglow.
Caching secrets, swift and bright,
Ignoring privates, a new delight!
With threads that dance and futures that gleam,
Our downloader's now a dream! 🌟


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4428941 and b0fbc2b.

Files selected for processing (4)
  • src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/AbstractQbittorrent.java (9 hunks)
  • src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/impl/enhanced/QBittorrentEE.java (5 hunks)
  • webui/src/views/dashboard/locale/en-US.ts (1 hunks)
  • webui/src/views/dashboard/locale/zh-CN.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/AbstractQbittorrent.java
  • webui/src/views/dashboard/locale/en-US.ts
  • webui/src/views/dashboard/locale/zh-CN.ts
Additional context used
Learnings (1)
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/impl/enhanced/QBittorrentEE.java (1)
Learnt from: Ghost-chu
PR: PBH-BTN/PeerBanHelper#400
File: src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java:134-142
Timestamp: 2024-08-30T18:57:41.277Z
Learning: Ghost-chu prefers not to receive logging suggestions in code reviews.
Additional comments not posted (5)
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/impl/enhanced/QBittorrentEE.java (5)

1-6: LGTM!

The updated package declaration and import statements align with the refactoring of the class hierarchy and configuration management.


Line range hint 20-37: LGTM!

The additional Lombok annotations and the updated class declaration to extend AbstractQbittorrent align with the refactoring of the class structure and hierarchy.


Line range hint 40-73: LGTM!

The updated constructor, static factory methods, and login0 method align with the introduction of QBittorrentEEConfigImpl and the shadow ban functionality. The changes enhance the login process and provide additional checks for shadow ban if enabled.


Line range hint 110-134: LGTM!

The updated getPeers method utilizes QBittorrentEEPeer and includes additional checks to filter out invalid or unwanted peers. The workaround for the qBittorrent localization issue ensures consistent handling of peer IDs and client names. These changes enhance the quality and reliability of the returned peer list.


Line range hint 229-235: LGTM!

The updated test method in the BanHandlerShadowBan class ensures that the shadow ban functionality is properly enabled by retrieving and parsing the qBittorrent preferences. Caching the shadowBanEnabled flag optimizes the check by avoiding unnecessary API calls.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java (1)

230-257: Good implementation of the caching logic!

  • The method correctly checks the cache first and queries the API only if necessary.
  • Storing the retrieved privacy status in the cache helps avoid unnecessary API calls in the future.

Consider improving the error handling by propagating the exception or providing a fallback value instead of just logging the error. This would allow the caller to handle the error appropriately.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a67d1c7 and d9311ac.

Files selected for processing (6)
  • src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrent.java (3 hunks)
  • src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java (6 hunks)
  • webui/src/components/forms/qbittorrent.vue (0 hunks)
  • webui/src/components/forms/qbittorrentee.vue (0 hunks)
  • webui/src/views/dashboard/locale/en-US.ts (0 hunks)
  • webui/src/views/dashboard/locale/zh-CN.ts (0 hunks)
Files not reviewed due to no reviewable changes (4)
  • webui/src/components/forms/qbittorrent.vue
  • webui/src/components/forms/qbittorrentee.vue
  • webui/src/views/dashboard/locale/en-US.ts
  • webui/src/views/dashboard/locale/zh-CN.ts
Additional context used
Learnings (1)
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java (1)
Learnt from: Ghost-chu
PR: PBH-BTN/PeerBanHelper#400
File: src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java:134-142
Timestamp: 2024-08-30T18:57:41.277Z
Learning: Ghost-chu prefers not to receive logging suggestions in code reviews.
Additional comments not posted (10)
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrent.java (5)

37-38: LGTM!

The imports are necessary for the usage of concurrency utilities and stream operations in the code changes.


48-55: LGTM!

The introduction of the isPrivateCacheMap, isPrivateExecutorService, and isPrivateSemaphore fields is a good approach to optimize performance and limit concurrent requests.

  • The isPrivateCacheMap efficiently caches the privacy status of torrents, reducing the need for repeated API calls.
  • The isPrivateExecutorService allows for concurrent processing of API requests, improving performance.
  • The isPrivateSemaphore effectively limits the number of concurrent requests to prevent overloading the server.

169-185: LGTM!

The use of CompletableFuture for asynchronous processing of private field checks is a good approach to improve performance.

  • The isPrivateSemaphore is properly acquired and released to limit concurrent access.
  • The futures list stores the CompletableFuture objects for later processing.
  • The code segment effectively handles the case when the is_private field is missing and private torrents should be ignored.

190-204: LGTM!

The processing of the CompletableFuture results using a stream pipeline is a clean and efficient approach.

  • CompletableFuture.allOf ensures that all the asynchronous tasks are completed before processing the results.
  • The stream pipeline effectively filters and maps the QBTorrent objects to TorrentImpl objects, extracting the necessary fields.
  • The code segment correctly handles the case when ignorePrivate is true and filters out private torrents.

209-236: LGTM!

The checkAndSetPrivateField method is a well-implemented solution for checking and setting the private field of a QBTorrent object.

  • The use of caching in the isPrivateCacheMap avoids repeated API calls for the same torrent, improving performance.
  • The synchronization of the isPrivateCacheMap using synchronized ensures thread safety when updating the cache.
  • The error handling and logging are appropriate for the API request.
src/main/java/com/ghostchu/peerbanhelper/downloader/impl/qbittorrent/QBittorrentEE.java (5)

38-39: LGTM!

The new imports are necessary for the changes made in the file and look good.


50-57: Great job with the caching and concurrency handling!

  • The LinkedHashMap cache implementation with a maximum size and automatic removal of the oldest entry is a good approach to limit memory usage.
  • Using ExecutorService to manage concurrent API requests is a good practice to improve performance and avoid blocking the main thread.
  • The Semaphore is used correctly to limit the number of concurrent accesses and prevent overwhelming the API.

190-226: Excellent implementation of asynchronous privacy checks!

  • Using CompletableFuture to asynchronously check the privacy status is a good approach to improve performance and avoid blocking the main thread.
  • The isPrivateSemaphore is used correctly to limit the number of concurrent accesses.
  • Waiting for all futures to complete using CompletableFuture.allOf ensures that all privacy checks are done before proceeding.
  • Filtering the results based on the ignorePrivate configuration is implemented correctly.

481-481: LGTM!

The new ignorePrivate field is added correctly to the Config class.


500-500: LGTM!

The ignorePrivate field is correctly read from and written to the YAML configuration in the Config class.

Also applies to: 516-516

@Ghost-chu
Copy link
Collaborator

@Ghost-chu
Copy link
Collaborator

补丁更新到 PR 后,请注意合并上游 master 的新更改。合并完成后进行 CI 测试。

@ccloli
Copy link
Contributor Author

ccloli commented Sep 15, 2024

请尝试应用此补丁后更新到 PR

已处理,不过 patch 文件不像是标准的 git format-patch,里面没有 From: 之类的信息,git am 会失败。所以我擅自 apply 后直接 git commit,并手动指定了 author.nameauthor.email 为您的 GitHub no-reply 邮箱。

如果对此有问题的话(例如上面的 Unverified),可以回复我做 git reset --hard 后重新提交。另外这个 PR 已经开启了 Allow edits and access to secrets by maintainers,所以有需要的话您也可以直接提交到该 PR 所在的 remote 分支。

@Ghost-chu Ghost-chu requested a review from paulzzh September 15, 2024 17:17
@Ghost-chu
Copy link
Collaborator

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Sep 15, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Collaborator

@Ghost-chu Ghost-chu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebUI 的 Lint 检查未通过,需要修复一下

@Ghost-chu
Copy link
Collaborator

请合并一下上游更改,PR 冲突了

@ccloli
Copy link
Contributor Author

ccloli commented Sep 16, 2024

请合并一下上游更改,PR 冲突了

已合并,不过先前的 patch 文件里将 QBPeerQBEEPeer 重命名为了 QBittorrentPeerQBittorrentEEPeer,所以合并后对冲突的部分沿用了后者,即 QBittorrentPeerQBittorrentEEPeer。目前构建是通过的,运行看起来也没有问题,若有疑问可以重新改回 master 里的命名。

@Ghost-chu Ghost-chu enabled auto-merge September 16, 2024 08:33
@Ghost-chu Ghost-chu disabled auto-merge September 16, 2024 08:36
@Ghost-chu
Copy link
Collaborator

LGTM

@Ghost-chu Ghost-chu merged commit 4b38a04 into PBH-BTN:master Sep 16, 2024
6 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] 支持 qBittorrent 4.x 下排除私有 torrent
2 participants