-
Notifications
You must be signed in to change notification settings - Fork 74
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
Make sure we're connected before starting requests #2297
Conversation
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.
Automatically approving tomaka's pull requests. This auto-approval will be removed once more maintainers are active.
twiggy diff reportDifference in .wasm size before and after this pull request.
|
src/libp2p/peers.rs
Outdated
@@ -1171,6 +1171,20 @@ where | |||
self.inner.respond_in_request(id.0, response) | |||
} | |||
|
|||
/// Returns `true` if there exists an established connection with the given peer. | |||
pub fn has_established_connection(&self, peer: &PeerId) -> bool { |
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.
pub fn has_established_connection(&self, peer: &PeerId) -> bool { | |
pub fn has_established_connection(&self, peer_id: &PeerId) -> bool { |
None => return false, | ||
}; | ||
|
||
self.connections_by_peer |
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.
why would we want to have multiple connections to the same peer?
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.
This can happen accidentally if two peers connect to each other at the same time.
Another situation is if you connect to an IP address and expect to find peer A but actually connect to peer B, even though you're already connected to B.
In that case, from the point of view of peer B, maybe you have accidentally connected to it (the example that I just gave), but maybe the first connection you had with B is actually dead and you're reopening a new one.
The best way to handle these situations is to allow multiple connections between peers, and automatically close unused ones.
src/network/service.rs
Outdated
@@ -1919,6 +1919,11 @@ where | |||
None | |||
} | |||
|
|||
/// Returns `true` if there exists an established connection with the given peer. | |||
pub fn has_established_connection(&self, peer: &PeerId) -> bool { |
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.
pub fn has_established_connection(&self, peer: &PeerId) -> bool { | |
pub fn has_established_connection(&self, peer_id: &PeerId) -> bool { |
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.
👍
Fix #2295
After #2264, we now panic if starting a request on a peer we're not connected to because this is an invalid usage of the API.
The reason why this wouldn't panic before is because there was always a chance of a race condition where a connection started being closed in parallel of the request being started.
We simply do the check for connectivity in the network service, as it is now possible to do so.