Skip to content

feat(primitives): replace TORRENT_PEERS_LIMIT with AnnouncePolicy::max_peers_per_announce - #1877

Merged
josecelano merged 2 commits into
torrust:developfrom
josecelano:1864-1669-review-torrent-peers-limit
Jun 4, 2026
Merged

josecelano merged 2 commits into
torrust:developfrom
josecelano:1864-1669-review-torrent-peers-limit

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

Closes #1864 — sub-task of EPIC #1669.

Move the peer-count cap from a global compile-time constant (TORRENT_PEERS_LIMIT = 74) to a configurable field AnnouncePolicy::max_peers_per_announce (default 74, serialized via serde). This makes the limit observable in the configuration file and overridable at runtime without recompilation.

Changes

  • Remove TORRENT_PEERS_LIMIT from primitives::policy and its re-export from primitives::lib
  • Add AnnouncePolicy::max_peers_per_announce: usize (serde default = 74, stored in [core.announce_policy] config section)
  • Refactor PeersWanted in tracker-core:
    • Remove From<i32> and From<u32> impls (were silently applying the global cap at construction time)
    • Add PeersWanted::from_client_request(i32) — ≤0 → AsManyAsPossible, positive → Only { amount }
    • PeersWanted::limit() now takes an explicit max_peers: usize argument, applied at announce time from announce_policy.max_peers_per_announce
  • Fix get_torrent_peers — now takes max_peers: usize instead of using the global constant (old implementation had a bug: max(limit, TORRENT_PEERS_LIMIT) returned more than requested)
  • Update all call sites, tests, configuration fixtures, and docs
  • Record decision as DEC-10 in docs/issues/open/1669-overhaul-packages/DECISIONS.md

Configuration

The new field appears in [core.announce_policy]:

[core.announce_policy]
interval = 120
interval_min = 120
max_peers_per_announce = 74

Existing configs without the field continue to work (serde default = 74).

Testing

  • All workspace tests pass (cargo test --workspace)
  • Pre-push checks pass (nightly format, nightly check, nightly docs, full stable test suite)

…x_peers_per_announce

Move the peer-count cap from a global constant to a configurable field on
`AnnouncePolicy`. The new `max_peers_per_announce: usize` field (default 74)
is serialized via serde and respected by `PeersWanted::limit(max_peers)` at
announce time.

- Remove `TORRENT_PEERS_LIMIT` from `primitives::policy`
- Add `AnnouncePolicy::max_peers_per_announce` (serde default = 74)
- Replace `PeersWanted::From<i32/u32>` impls with `from_client_request(i32)`
- `PeersWanted::limit()` now takes an explicit `max_peers: usize` argument
- `get_torrent_peers` takes `max_peers: usize` instead of using the global
- Update all call sites, tests, configuration fixtures, and docs

Closes torrust#1864
Copilot AI review requested due to automatic review settings June 4, 2026 16:13
@josecelano josecelano self-assigned this Jun 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR makes the per-announce peer return cap runtime-configurable by moving it from the hardcoded TORRENT_PEERS_LIMIT = 74 constant to a new AnnouncePolicy::max_peers_per_announce config field (default 74). It also refactors PeersWanted so the cap is applied explicitly at announce time (using injected policy), and updates call sites, tests, docs, and the EPIC decision log accordingly.

Changes:

  • Replace global TORRENT_PEERS_LIMIT with AnnouncePolicy::max_peers_per_announce (serde default 74) and plumb it through announce-time limiting.
  • Refactor PeersWanted to remove implicit From<i32/u32> capping and introduce from_client_request(i32) + limit(max_peers).
  • Update repositories/tests/docs to use explicit limits and record the decision as DEC-10.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/udp-tracker-core/src/services/announce.rs Switch to PeersWanted::from_client_request(...) constructor.
packages/udp-server/src/handlers/announce.rs Update tests for new get_torrent_peers(..., max_peers) signature.
packages/tracker-core/tests/integration.rs Extend AnnouncePolicy test fixture with max_peers_per_announce.
packages/tracker-core/src/torrent/repository/in_memory.rs Remove global cap usage and accept explicit limit/max_peers parameters.
packages/tracker-core/src/announce_handler.rs Inject policy cap at announce time; refactor PeersWanted API + tests.
packages/torrent-repository-benchmarking/tests/entry/mod.rs Replace TORRENT_PEERS_LIMIT usage with a local max-peers constant in tests.
packages/swarm-coordination-registry/src/swarm/registry.rs Update docs/tests to remove dependency on the deleted constant.
packages/primitives/src/policy.rs Remove TORRENT_PEERS_LIMIT from primitives policy module.
packages/primitives/src/lib.rs Stop re-exporting TORRENT_PEERS_LIMIT.
packages/primitives/src/announce.rs Add AnnouncePolicy::max_peers_per_announce with serde default + docs.
packages/configuration/src/v2_0_0/mod.rs Document/config fixture update to include max_peers_per_announce.
packages/axum-http-server/tests/server/v1/contract.rs Update tests for new get_torrent_peers(..., max_peers) signature.
packages/axum-http-server/src/lib.rs Update HTTP tracker docs to reference configurable max peers.
docs/issues/open/1864-1669-review-torrent-peers-limit/ISSUE.md Mark acceptance criteria as completed.
docs/issues/open/1669-overhaul-packages/DECISIONS.md Add DEC-10 describing the design decision and rationale.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/axum-http-server/src/lib.rs Resolved
- swarm-coordination-registry: fix doc comment on get_peers_peers_excluding
  to accurately describe the limit parameter (not 'greater of provided or
  configured max' — simply capped by the limit argument)
- axum-http-server: fix rustdoc link to point at the field
  AnnouncePolicy::max_peers_per_announce rather than the type
- torrent-repository-benchmarking: assert against MAX_PEERS constant
  instead of hardcoded 74 to keep test consistent if the limit changes
@josecelano

Copy link
Copy Markdown
Member Author

ACK 388e3d1

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.83%. Comparing base (d9ec549) to head (388e3d1).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1877   +/-   ##
========================================
  Coverage    80.83%   80.83%           
========================================
  Files          368      368           
  Lines        27580    27575    -5     
  Branches     27580    27575    -5     
========================================
- Hits         22294    22290    -4     
  Misses        4983     4983           
+ Partials       303      302    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano
josecelano merged commit 0723daa into torrust:develop Jun 4, 2026
26 checks passed

This branch was previously deployed

1 inactive deployment
coverage — 388e3d1f Deployed Jun 4, 2026 by josecelano via Generate Coverage Report #1361
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review and refactor TORRENT_PEERS_LIMIT: hardcoded constant vs. config option

2 participants