Skip to content

Simplify ConnectionHandler trait by removing as many associated types as possible #2863

Open

Description

@thomaseizinger

Simplify the ConnectionHandler interface. This is one of the first interfaces users get in touch with when the implement their own protocols. Making it as simple as possible without removing functionality should be the goal. All but {To,From}Behaviour should be removed and we should directly hand the negotiated stream to the ConnectionHandler without any form of "upgrade".

### Sub-epics
- [ ] https://github.com/libp2p/rust-libp2p/issues/4306
- [ ] https://github.com/libp2p/rust-libp2p/issues/3268
- [ ] https://github.com/libp2p/rust-libp2p/discussions/4585
- [ ] https://github.com/libp2p/rust-libp2p/issues/3591
### Use `ReadyUpgrade` everywhere
- [ ] https://github.com/libp2p/rust-libp2p/issues/4075
- [ ] https://github.com/libp2p/rust-libp2p/pull/4563
- [ ] https://github.com/libp2p/rust-libp2p/pull/3914
- [ ] https://github.com/libp2p/rust-libp2p/issues/4649
- [ ] refactor(dcutr): use `ReadyUpgrade` for `{In,Out}boundUpgrade`
- [ ] refactor(gossipsub): use `ReadyUpgrade` for `{In,Out}boundUpgrade`
### Migration plan
- [ ] https://github.com/libp2p/rust-libp2p/issues/4307
- [ ] https://github.com/libp2p/rust-libp2p/issues/4521
- [ ] https://github.com/libp2p/rust-libp2p/issues/4697
- [ ] https://github.com/libp2p/rust-libp2p/issues/4790

Activity

  1. thomaseizinger commented on Sep 2, 2022

    @thomaseizinger
    Author
  2. thomaseizinger commented on Sep 2, 2022

    @thomaseizinger
    Author
  3. thomaseizinger commented on Sep 2, 2022

    @thomaseizinger
    Author
  4. mxinden commented on Sep 6, 2022

    @mxinden
  5. mxinden commented on Sep 12, 2022

    @mxinden
  6. thomaseizinger commented on Oct 19, 2022

    @thomaseizinger
    Author
  7. mxinden commented on Oct 21, 2022

    @mxinden
  8. thomaseizinger commented on Dec 19, 2022

    @thomaseizinger
    Author
  9. thomaseizinger commented on Jan 10, 2023

    @thomaseizinger
    Author
  10. thomaseizinger commented on Jan 11, 2023

    @thomaseizinger
    Author
  11. mxinden commented on Jan 18, 2023

    @mxinden
  12. modified the milestone: on Sep 18, 2023
  13. changed the title [-]swarm/handler: Decouple `ConnectionHandler` from `{In,Out}boundUpgrade`[/-] [+]Decouple `ConnectionHandler` from `{In,Out}boundUpgrade`[/+] on Sep 20, 2023
  14. changed the title [-]Decouple `ConnectionHandler` from `{In,Out}boundUpgrade`[/-] [+]Simplify `ConnectionHandler` trait by removing as many associated types as possible[/+] on Sep 20, 2023
  15. RGBCube commented on Oct 18, 2025

    @RGBCube

    A question I have is would it be possible to remove poll and add an async fn that gets run until completion? Traits have async fn support without boxing now, and I don't think libp2p needs the handlers to be dyn-safe. Would remove a lot of allocations (that are very hard to avoid without janky nightly features) and make code much cleaner too. This would need a new major version.

  16. dariusc93 commented on Oct 18, 2025

    @dariusc93
    Member

    A question I have is would it be possible to remove poll and add an async fn that gets run until completion? Traits have async fn support without boxing now, and I don't think libp2p needs the handlers to be dyn-safe. Would remove a lot of allocations (that are very hard to avoid without janky nightly features) and make code much cleaner too. This would need a new major version.

    an async desugar to impl Future and await is no different than polling in place so it would probably be better to leave things as is. There are some aspects of the codebase that uses async-trait that could use RIIT instead (ie request-response) but other parts may still need to be boxed.

  17. thomaseizinger commented on Oct 19, 2025

    @thomaseizinger
    ContributorAuthor

    A question I have is would it be possible to remove poll and add an async fn that gets run until completion? Traits have async fn support without boxing now, and I don't think libp2p needs the handlers to be dyn-safe. Would remove a lot of allocations (that are very hard to avoid without janky nightly features) and make code much cleaner too. This would need a new major version.

    The main and still unsolved issue of async fn are mutable borrows. You can only construct a single async fn that borrows something mutably. That makes concurrency pretty difficult.

    Using something like FuturesUnordered or the utilities from futures-bounded etc makes it pretty ergonomic and still allows for good (and controlled!) concurrency.

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

    tracking-issueIssues which are the entry point to bigger projects.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions