|
| 1 | +--- |
| 2 | +doc-type: issue |
| 3 | +issue-type: task |
| 4 | +status: open |
| 5 | +priority: p3 |
| 6 | +github-issue: 1864 |
| 7 | +spec-path: docs/issues/open/1864-1669-review-torrent-peers-limit/ISSUE.md |
| 8 | +branch: null |
| 9 | +related-pr: null |
| 10 | +last-updated-utc: 2026-06-01 00:00 |
| 11 | +semantic-links: |
| 12 | + skill-links: |
| 13 | + - create-issue |
| 14 | + related-artifacts: |
| 15 | + - packages/primitives/src/policy.rs |
| 16 | + - packages/tracker-core/src/announce_handler.rs |
| 17 | + - packages/tracker-core/src/torrent/repository/in_memory.rs |
| 18 | + - packages/swarm-coordination-registry/src/swarm/registry.rs |
| 19 | + - packages/axum-http-server/src/lib.rs |
| 20 | + - docs/issues/open/1669-overhaul-packages/EPIC.md |
| 21 | + - docs/issues/open/1669-overhaul-packages/DECISIONS.md |
| 22 | +--- |
| 23 | + |
| 24 | +<!-- skill-link: create-issue --> |
| 25 | + |
| 26 | +# Issue #1864 — Review and refactor `TORRENT_PEERS_LIMIT`: hardcoded constant vs. config option |
| 27 | + |
| 28 | +## Goal |
| 29 | + |
| 30 | +Decide whether `TORRENT_PEERS_LIMIT` should remain a global compile-time constant, |
| 31 | +be localized to each consuming package, or become a runtime configuration field. |
| 32 | +Record the decision and implement it. |
| 33 | + |
| 34 | +This is a follow-up to issue [#1859](../closed/1859-1669-move-tracker-policy-and-private-mode-to-primitives/ISSUE.md) |
| 35 | +and a sub-task of EPIC [#1669](../open/1669-overhaul-packages/EPIC.md). |
| 36 | + |
| 37 | +## Background |
| 38 | + |
| 39 | +Issue #1859 moved `TORRENT_PEERS_LIMIT` (`74`) and `TrackerPolicy` from |
| 40 | +`torrust-tracker-configuration` into `torrust-tracker-primitives`. That was the |
| 41 | +right first step to break the configuration coupling, but the constant is still a |
| 42 | +global value shared across multiple packages. |
| 43 | + |
| 44 | +### Current usages |
| 45 | + |
| 46 | +`TORRENT_PEERS_LIMIT` is used in three distinct roles: |
| 47 | + |
| 48 | +1. **Parse-time cap in `From<i32/u32> for PeersWanted`** (announce handler): |
| 49 | + |
| 50 | + ```rust |
| 51 | + // packages/tracker-core/src/announce_handler.rs |
| 52 | + impl From<i32> for PeersWanted { |
| 53 | + fn from(value: i32) -> Self { |
| 54 | + ... |
| 55 | + PeersWanted::Only { amount: amount.min(TORRENT_PEERS_LIMIT) } |
| 56 | + } |
| 57 | + } |
| 58 | + ``` |
| 59 | + |
| 60 | + Because this is a `From` impl, runtime injection is not possible — the limit is |
| 61 | + baked in at the trait boundary. |
| 62 | + |
| 63 | +2. **Default return count in `PeersWanted::limit()`** — returned when the client |
| 64 | + requested `AsManyAsPossible`. |
| 65 | + |
| 66 | +3. **Query cap in repository methods** — `in_memory.rs` and `swarm/registry.rs` |
| 67 | + call `get_peers` / `get_swarm_peers` with `TORRENT_PEERS_LIMIT` as the hard |
| 68 | + ceiling. |
| 69 | + |
| 70 | +## Questions to Resolve |
| 71 | + |
| 72 | +- Should `TORRENT_PEERS_LIMIT` remain a single global constant, or should each |
| 73 | + package define its own local default? |
| 74 | +- Should the cap become a runtime configuration option (e.g., a field on |
| 75 | + `TrackerPolicy`) so it can be tuned per deployment without recompilation? |
| 76 | +- For the `From<i32/u32> for PeersWanted` trait impls, which cannot accept injected |
| 77 | + state, is a package-local constant the right answer, or should the impls be |
| 78 | + replaced by explicit constructors / free functions that accept the limit? |
| 79 | +- If it becomes a config option, where does it sit in the configuration hierarchy |
| 80 | + and how is it threaded through to the repository query methods? |
| 81 | + |
| 82 | +## Possible Approaches |
| 83 | + |
| 84 | +| Approach | Pros | Cons | |
| 85 | +| ----------------------------------------------------- | ---------------------------------- | ----------------------------------------------------------- | |
| 86 | +| Keep global constant in `primitives` (current state) | Simple, no API churn | Magic number, not tunable, couples packages | |
| 87 | +| Move constant into each consuming package | Removes cross-package coupling | Duplication, values can drift | |
| 88 | +| Add `max_peers_per_announce` field to `TrackerPolicy` | Runtime-tunable, operator-visible | Requires plumbing through announce handler and repositories | |
| 89 | +| Replace `From` impls with explicit constructors | Removes implicit global dependency | API change for callers | |
| 90 | + |
| 91 | +## Acceptance Criteria |
| 92 | + |
| 93 | +- [ ] A decision (ADR or `DECISIONS.md` entry under EPIC #1669) recording the chosen |
| 94 | + approach and the rationale. |
| 95 | +- [ ] If the decision is to change the current design: implementation is complete, |
| 96 | + all tests pass, and the doc reference in `axum-http-server/src/lib.rs` is updated. |
| 97 | +- [ ] `cargo test --workspace` passes. |
| 98 | +- [ ] `linter all` passes. |
0 commit comments