Skip to content

Implement BEP 40 Canonical Peer Priority #165

Closed

Description

@Nehliin

Necessary to make #91 efficient and just a good standard to follow

We should take this prioritization in to account when handling the ConnectToPeers messages to begin with.

Activity

  1. added
    enhancement
    on Oct 19, 2026
  2. tkalum commented on Oct 20, 2026

    @tkalum
    Contributor

    Hi @Nehliin, as discussed in #91, I'd like to take on this issue. I'm mostly familiar with the CLI and still getting used to the core code, so I'll ask any specific questions here. Thanks!

  3. tkalum commented on Oct 25, 2026

    @tkalum
    Contributor

    Hey @Nehliin, quick question about implementing the sorting, I wanted to double-check a couple of structural things:

    I added my new PeerPriority struct inside src/peer_comm/canonical_priority.rs to handle the calculations. Let me know if that's the right module/file layout.

    For applying it, I found the Command::ConnectToPeers arm inside handle_commands in bittorrent/src/event_loop.rs:

    for addr in addrs.into_iter().map(|addr| addr.into()) {
        connection_manager.maybe_connect_to_peer(addr, io);
    }

    Should I sort and check the priorities right here in this loop before connecting, or is that sorting logic supposed to happen somewhere else (like connection manager) instead?

    Also, to calculate the priority we need our own listening SocketAddr (our_addr) as one of the inputs. Where can I find that inside the event loop?

  4. Nehliin commented on Oct 25, 2026

    @Nehliin
    OwnerAuthor

    I do not currently have a strong opinion where the calculation can live in the structure so peer_comm/canonical_priority could work! Start with that and we can evaluate during PR review later on.

    Yes! That's where I want the prioritization to take effect, but I think it makes more sense to move the logic into the connection manager itself. So I think we should change the api for the connection_manager to instead accept a list of peers and both sort + determine if they have been banned or not. You can add a connect_to_peers method that accepts the list and under the hood does the snippet you posted + the sorting.

    The listening adder of the socket can be propagated to the ConnectionManager::new function and it can save it internally for future calculations. Look at the run method inside of the EventLoop there you see the listener being provided as an argument. Use that to propagate the SocketAddr to the connection manager.

    Hope that clarifies things! Let me know if you have more questions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions