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

Improve connection management #266

Merged

Conversation

tnull
Copy link
Collaborator

@tnull tnull commented Mar 4, 2024

Based on #243.
Based on #244.

As a pre-factor to #256, we clean up our connection handling. Namely, we move it to a new connection.rs file and clean up the logic a bit, allowing us to drop the dependency on the futures crate.

Then, we introduce a ConnectionManager that handles pending connection attempts. Previously, concurrent calls to
do_connect_peer/connect_peer_if_necessary could result in multiple connections being opened, just to be closed as redundant shortly after. Here, we fix this behavior by ensuring only one connection attempt is inflight at any given point in time.

@tnull tnull requested a review from jkczyz March 4, 2024 13:55
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch 2 times, most recently from 7b4b7d5 to 5b56a8b Compare March 4, 2024 14:33
@tnull tnull mentioned this pull request Mar 4, 2024
Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commits specific to this PR LGTM. Didn't review the commits from based PRs.

Also, in commit message for 5b56a8b:

s/task is responsible/task responsible

let node_id_b = node_b.node_id();
let node_addr_b = node_b.listening_addresses().unwrap().first().unwrap().clone();

std::thread::sleep(std::time::Duration::from_secs(1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment on why the sleep is needed? Is there any way to avoid it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment here as well as to do_connection_restart_behavior. The sleep is necessary to allow the listening task of the connected node to spawn and bind successfully before we try connecting to it.

It could be avoidable if we introduced a mechanism to Node::start that would wait for the task to come up. However, I currently don't think it's worth it as on the contrary we try to keep Node::startas quick as possible, i.e., try to push everything possible to background tasks in order to improve startup time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have an is_listening method, even if only for testing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, since this is essentially #127 I now prioritized that in #272 and rebased this PR on top, now using the new node.status().is_listening field.

@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch from 5b56a8b to 5c9e930 Compare March 5, 2024 08:33
@tnull tnull mentioned this pull request Mar 5, 2024
19 tasks
@tnull tnull added this to the 0.3 milestone Mar 5, 2024
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch from 5c9e930 to 785543d Compare March 5, 2024 08:47
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch 3 times, most recently from e781c08 to d60f009 Compare March 7, 2024 12:52
src/lib.rs Outdated Show resolved Hide resolved
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch from d60f009 to c971535 Compare March 8, 2024 07:44
@tnull
Copy link
Collaborator Author

tnull commented Mar 8, 2024

Rebased after #272 was merged to resolve minor conflicts.

@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch 2 times, most recently from 2efc3b9 to f2f1ab2 Compare March 12, 2024 10:03
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch from f2f1ab2 to 7d6e260 Compare April 23, 2024 07:26
@tnull
Copy link
Collaborator Author

tnull commented Apr 23, 2024

Rebased on #244.

@tnull tnull requested a review from jkczyz April 23, 2024 07:28
src/connection.rs Outdated Show resolved Hide resolved
src/connection.rs Outdated Show resolved Hide resolved
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch from eac8e26 to 85a6803 Compare April 25, 2024 09:09
tnull added 5 commits April 25, 2024 20:34
.. just a minor cleanup to further modularize the codebase. Also, we'll
be reusing these methods in `Event::ConnectionNeeded` soon.
.. we should consider dropping `Deref` and instead just commiting to
store a `Arc<L>` everwhere, as it gets tedious to maintain.

However, this is barely scraping by and the least invasive change here.
.. which is a bit more readable and in-line what we do other places,
plus it allows us to drop the `futures` dependency.
... we check that we can successfully issue concurrent connection
attempts, which all succeed.
Previously, concurrent calls to
`do_connect_peer`/`connect_peer_if_necessary` could result in multiple
connections being opened, just to be closed as redundant shortly after.
Parallel connection attempt were therefore prone to fail.

Here, we introduce a `ConnectionManager` that implements a pub/sub
pattern: upon the initial call, the task responsible for polling the
connection future to completion registers that a connection is
in-flight. Any calls following will check this and register a `oneshot`
channel to be notified of the `Result`.
@tnull tnull force-pushed the 2024-03-introduce-connection-manager branch from 85a6803 to 77c538b Compare April 25, 2024 18:36
@tnull
Copy link
Collaborator Author

tnull commented Apr 25, 2024

Rebased on main and squashed fixups without further changes.

@tnull tnull merged commit 640a1fd into lightningdevkit:main Apr 25, 2024
12 of 13 checks passed
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.

2 participants