Skip to content

Review and refactor TORRENT_PEERS_LIMIT: hardcoded constant vs. config option #1864

Closed

Description

@josecelano

Context

Issue #1859 moved TORRENT_PEERS_LIMIT (value: 74) and TrackerPolicy from torrust-tracker-configuration into torrust-tracker-primitives. That was the right first step to break a dependency cycle, but the constant is still a global hardcoded value spread across several packages.

Problem

TORRENT_PEERS_LIMIT is currently used in at least three distinct roles:

  1. Cap inside From<i32/u32> for PeersWanted — enforces a maximum on client-requested peers at parse time:

    // packages/tracker-core/src/announce_handler.rs
    impl From<i32> for PeersWanted {
        fn from(value: i32) -> Self {
            ...
            PeersWanted::Only { amount: amount.min(TORRENT_PEERS_LIMIT) }
        }
    }

    Because this is a From impl, there is no way to inject a runtime limit — the constant is baked in.

  2. Default return count in PeersWanted::limit() — used when the client requested AsManyAsPossible.

  3. Query cap in repository get_peers / get_swarm_peers — used in in_memory.rs and swarm/registry.rs to limit how many peers are returned per query.

The constant living in primitives is better than in configuration, but it is still a global that couples unrelated packages to a single magic number.

Questions to Resolve

  • Should TORRENT_PEERS_LIMIT remain a single global constant, or should each package define its own local default?
  • Should the cap be a runtime configuration option (e.g., TrackerPolicy::max_peers_per_announce) instead of a compile-time constant? This would make it tunable per deployment without recompilation.
  • For the From<i32> for PeersWanted trait impl, which cannot accept injected state, is a package-local constant the right answer, or should the trait impl be replaced by a free function / builder that accepts the limit explicitly?
  • If it becomes a config option, where does it live in the configuration hierarchy, and how do the repository query methods receive it?

Possible Approaches

Approach Pros Cons
Keep global constant in primitives (status quo post-#1859) Simple, no API churn Magic number, not tunable, couples packages
Move constant into each consuming package Removes cross-package coupling Duplication, value can drift
Make it a TrackerPolicy field with a default Runtime-tunable, operator-visible Requires plumbing through announce handler and repositories
Replace From impls with explicit constructors Removes implicit global dependency API change for callers

Related

Expected Output

A decision (either an ADR or a DECISIONS.md entry under the #1669 EPIC) recording which approach is chosen, followed by the implementation if the decision is to change the current design.

Activity

  1. added theissue type on Jun 1, 2026
  2. self-assigned this
    on Jun 1, 2026
  3. added 3 commits that reference this issue on Jun 2, 2026
  4. added 3 commits that reference this issue on Jun 4, 2026
    0723daa
    f34fddc
    78e0c00
  5. added a commit that references this issue on Jul 20, 2026
    dc470fc
  6. added 3 commits that reference this issue on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions