-
Notifications
You must be signed in to change notification settings - Fork 1k
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
swarm/src/lib: Prioritize Behaviour over Pool and Pool over Listeners #2627
Conversation
Simplifies `PoolEvent`, no longer carrying a reference to an `EstablishedConnection` or the `Pool`, but instead the `PeerId`, `ConnectionId` and `ConnectedPoint` directly.
Have the main event loop (`Swarm::poll_next_event`) prioritize: 1. Work on `NetworkBehaviour` over work on `Pool`, thus prioritizing local work over work coming from a remote. 2. Work on `Pool` over work on `ListenersStream`, thus prioritizing work on existing connections over upgrading new incoming connections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! :)
@@ -1018,88 +1018,92 @@ where | |||
// across a `Deref`. | |||
let this = &mut *self; | |||
|
|||
// This loop polls the components below in a prioritized order. | |||
// | |||
// 1. [`NetworkBehaviour`] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Unless you are in rustdoc land (///
), I don't think this reference will do anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I am hoping for this to eventually become a feature and if not, at least it is consistent when find-replacing. I would keep it as is unless you feel strongly about it @thomaseizinger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No feeling strongly at all, just figured it might have been a mistake :)
swarm/src/lib.rs
Outdated
let mut listeners_not_ready = false; | ||
let mut connections_not_ready = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for getting rid of these flags!
return Poll::Pending | ||
} | ||
// Poll the listener(s) for new connections. | ||
match ListenersStream::poll(Pin::new(&mut this.listeners), cx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If ListenersStream
would actually implement Stream
then we could utilise poll_unpin
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, though we would have to handle the None
case which can never happen. rust-lang/futures-rs#1857 would be very useful here.
As an example @winksaville 426023e is one of those changes on a complex |
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
👍 Makes sense.
Not sure if I agree with this. Many NetworkBehaviours internally have a state that is influenced by events in the pool (e.g. list of connected peers) and the behaviour might rely on the fact that this state is really the most recent one. Also when the behaviour now initiates a dial-attempt, with |
As an aside a
True, though I would argue that this can happen anyways, thus each
But how would a
Good question. I should have included an example in the pull request description. Say we have a request-response style protocol. The remote is sending requests to the local node back-to-back. They are read from the socket in the corresponding Say that the Does the above example make sense @elenaf9? |
Thanks for the explanation @mxinden! Yes that makes sense, I agree with you then that |
By the way, long term it would be cool for a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Also very much in favor of splitting larger poll
methods into smaller chunks.
So you want Erlang-style selective receive from a Mailbox, or its close cousin join calculus. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
I guess so. Still have to give it more thought.
Will take a look. Thanks @rkuhn. |
One more thing that just came to my mind:
This is relevant for the very specific case that a behaviour internally holds a timer that is reset within those method calls. For almost all method calls this won't be a problem because a |
I think it does: Say that Lines 1082 to 1092 in f04f6bb
Thus The corresponding Lines 805 to 811 in f04f6bb
Given that the match arm does not return, Line 829 in f04f6bb
Back in Lines 1086 to 1090 in f04f6bb
Thus execution jumps to the top of the loop where the Lines 1031 to 1080 in f04f6bb
@elenaf9 am I missing something? |
Nope, I was missing that there was the |
By now I feel comfortable with the either |
Description
Builds on top of #2625.
Have the main event loop (
Swarm::poll_next_event
) prioritize:Work on
NetworkBehaviour
over work onPool
, thus prioritizinglocal work over work coming from a remote.
Work on
Pool
over work onListenersStream
, thus prioritizing workon existing connections over upgrading new incoming connections.
Refactors
NetworkBehaviourAction
,PoolEvent
andListenersEvent
handling along the way through 9f0ca95 87b7cb3 00346de to simplify the actual change, namely 426023e. Thus best reviewed one commit at a time.I will run this change on a larger machine to make sure it does not introduce any subtle errors.
Links to any relevant issues
Related to #2626.
Change checklist