Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swarm): async network behavior #5698

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

elenaf9
Copy link
Contributor

@elenaf9 elenaf9 commented Nov 28, 2024

Description

This PR drafts a generic async wrapper for network behaviors with asynchronous requests or queries (i.e. where the results for called methods are reported in swarm events).
It is inspired by #5294 and #5294 (comment).

The basic idea is:

  • the Async behavior wraps a network behavior and keeps track of pending requests
  • from a clonable Control interface commands can be sent to the Async behavior. The commands are mapped to methods of the inner network behavior.
  • the Async Behavior intercepts out events from the inner behavior and returns them directly through a result channel if they are among the pending queries. Else they are bubbled up to the swarm as usual.

The implementation is abstract over the inner network behavior, and only defines a trait QueryMapper that must be implemented for a network behavior. The goal is to have an async interface for all network behaviors that do any kind of long-running queries or requests.
I added basic POC support for kademlia. I think it would also work well for request-response.

@jxs was that roughly what you had in mind in #5294 (comment)?

Notes & open questions

As a consequence of keeping Async generic and independent from a concrete behavior, there are no convenient async mirrors of the behavior methods (e.g. get_closest_peers_async) like there is in #5294.
Instead, there is just one method for sending the Command from the control, and the single result type for all commands needs to be mapped/ unwrapped by the caller. However, the unpacking of the result is also how it currently needs to be done when polling the result of a query from the swarm.
Also, we could still add individual Control interfaces for the behaviors that wrap the generic Control and provide convenience functions.

Another alternative to the current trait design would be to use macros to generate async versions of a behavior.

Interested to hear what other folks think.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

Copy link
Contributor

mergify bot commented Nov 28, 2024

This pull request has merge conflicts. Could you please resolve them @elenaf9? 🙏

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.

1 participant