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): remove handler field from ConnectionClosed event #4076

Merged
merged 33 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
661717f
Remove `handler` field from `ConnectionClosed`
thomaseizinger Jun 15, 2023
892f4ce
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Jun 15, 2023
93ffddd
Fill in PR number
thomaseizinger Jun 15, 2023
36fa04b
Fix docs
thomaseizinger Jun 15, 2023
852c798
Add test
thomaseizinger Jun 15, 2023
7d69e10
Fix clippy warnings
thomaseizinger Jun 15, 2023
b5573d5
Update swarm/src/connection.rs
thomaseizinger Jun 15, 2023
889ee4e
Fix doc string
thomaseizinger Jun 19, 2023
70092a3
Document IO behaviour
thomaseizinger Jun 19, 2023
092d6af
Fix failing test
thomaseizinger Jun 19, 2023
9defe2c
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Sep 21, 2023
eb5e8e6
Fix new compile errors
thomaseizinger Sep 21, 2023
94fd012
Bump swarm-derive version
thomaseizinger Sep 21, 2023
9471a44
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Sep 24, 2023
7cf8550
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Sep 27, 2023
8dce53d
Merge branch 'feat/3046-poll-close' of github.com:libp2p/rust-libp2p …
thomaseizinger Sep 27, 2023
af4998b
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 10, 2023
69d9d05
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 15, 2023
e9449b8
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 16, 2023
93d1bc1
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 19, 2023
76a82cc
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 20, 2023
4684ef4
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 20, 2023
ea074f6
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 20, 2023
c48083a
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 20, 2023
cf28074
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 20, 2023
f58a53f
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 20, 2023
95c113a
Update docs
thomaseizinger Oct 22, 2023
79a5a68
Call `poll_close` to completion before terminating connection
thomaseizinger Oct 22, 2023
68403ae
Remove unused import
thomaseizinger Oct 22, 2023
06a5adb
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 24, 2023
f7dd0d2
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 24, 2023
bbc84c5
Fix compile error
thomaseizinger Oct 25, 2023
d035ce7
Merge branch 'master' into feat/3046-poll-close
thomaseizinger Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ where
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionClosed(_) => {}
FromSwarm::ConnectionEstablished(_) => {}
Expand Down
2 changes: 1 addition & 1 deletion misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl NetworkBehaviour for Behaviour {
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
Expand Down
6 changes: 2 additions & 4 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,14 @@ impl Behaviour {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
self.inner
.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));

Expand Down Expand Up @@ -536,7 +534,7 @@ impl NetworkBehaviour for Behaviour {
.handle_established_outbound_connection(connection_id, peer, addr, role_override)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses.on_swarm_event(&event);

match event {
Expand Down
4 changes: 2 additions & 2 deletions protocols/dcutr/src/behaviour_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Behaviour {
connection_id,
endpoint: connected_point,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if !connected_point.is_relayed() {
let connections = self
Expand Down Expand Up @@ -357,7 +357,7 @@ impl NetworkBehaviour for Behaviour {
Poll::Pending
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.external_addresses.on_swarm_event(&event);

match event {
Expand Down
4 changes: 2 additions & 2 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl Floodsub {
peer_id,
remaining_established,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if remaining_established > 0 {
// we only care about peer disconnections
Expand Down Expand Up @@ -478,7 +478,7 @@ impl NetworkBehaviour for Floodsub {
Poll::Pending
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.on_connection_established(connection_established)
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3135,7 +3135,7 @@ where
endpoint,
remaining_established,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
// Remove IP from peer scoring system
if let Some((peer_score, ..)) = &mut self.peer_score {
Expand Down Expand Up @@ -3484,7 +3484,7 @@ where
Poll::Pending
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.on_connection_established(connection_established)
Expand Down
4 changes: 0 additions & 4 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// Collection of tests for the gossipsub network behaviour

use super::*;
use crate::protocol::ProtocolConfig;
use crate::subscription_filter::WhitelistSubscriptionFilter;
use crate::transform::{DataTransform, IdentityTransform};
use crate::types::FastMessageId;
Expand Down Expand Up @@ -272,13 +271,10 @@ where
for connection_id in peer_connections.connections.clone() {
active_connections = active_connections.checked_sub(1).unwrap();

let dummy_handler = Handler::new(ProtocolConfig::default(), Duration::ZERO);

gs.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id: *peer_id,
connection_id,
endpoint: &fake_endpoint,
handler: dummy_handler,
remaining_established: active_connections,
}));
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl NetworkBehaviour for Behaviour {
Ok(self.discovered_peers.get(&peer))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
let listen_addr_changed = self.listen_addresses.on_swarm_event(&event);
let external_addr_changed = self.external_addresses.on_swarm_event(&event);

Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ where
remaining_established,
connection_id,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
self.connections.remove(&connection_id);

Expand Down Expand Up @@ -2433,7 +2433,7 @@ where
}
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses.on_swarm_event(&event);
let external_addresses_changed = self.external_addresses.on_swarm_event(&event);

Expand Down
2 changes: 1 addition & 1 deletion protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where
void::unreachable(ev)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses.on_swarm_event(&event);

match event {
Expand Down
3 changes: 1 addition & 2 deletions protocols/perf/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl NetworkBehaviour for Behaviour {
)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id, .. }) => {
self.connected.insert(peer_id);
Expand All @@ -164,7 +164,6 @@ impl NetworkBehaviour for Behaviour {
peer_id,
connection_id: _,
endpoint: _,
handler: _,
remaining_established,
}) => {
if remaining_established == 0 {
Expand Down
2 changes: 1 addition & 1 deletion protocols/perf/src/client/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl NetworkBehaviour for Behaviour {
Ok(Handler::default())
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id, .. }) => {
self.connected.insert(peer_id);
Expand Down
2 changes: 1 addition & 1 deletion protocols/perf/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl NetworkBehaviour for Behaviour {
)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.request_response.on_swarm_event(event);
}

Expand Down
2 changes: 1 addition & 1 deletion protocols/perf/src/server/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl NetworkBehaviour for Behaviour {
Ok(Handler::default())
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(_) => {}
FromSwarm::ConnectionClosed(_) => {}
Expand Down
2 changes: 1 addition & 1 deletion protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl NetworkBehaviour for Behaviour {
}
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(_)
| FromSwarm::ConnectionClosed(_)
Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Behaviour {
peer_id,
connection_id,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if let hash_map::Entry::Occupied(mut peer) = self.reservations.entry(peer_id) {
peer.get_mut().remove(&connection_id);
Expand Down Expand Up @@ -301,7 +301,7 @@ impl NetworkBehaviour for Behaviour {
)))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.external_addresses.on_swarm_event(&event);

match event {
Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Behaviour {
connection_id,
endpoint,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if !endpoint.is_relayed() {
match self.directly_connected_peers.entry(peer_id) {
Expand Down Expand Up @@ -193,7 +193,7 @@ impl NetworkBehaviour for Behaviour {
Ok(Either::Left(handler))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id,
Expand Down
2 changes: 1 addition & 1 deletion protocols/rendezvous/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl NetworkBehaviour for Behaviour {
.on_connection_handler_event(peer_id, connection_id, event);
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.external_addresses.on_swarm_event(&event);

self.inner.on_swarm_event(event);
Expand Down
2 changes: 1 addition & 1 deletion protocols/rendezvous/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl NetworkBehaviour for Behaviour {
}
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.inner.on_swarm_event(event);
}
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ where
connection_id,
remaining_established,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
let connections = self
.connected
Expand Down Expand Up @@ -765,7 +765,7 @@ where
))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.on_connection_established(connection_established)
Expand Down
19 changes: 3 additions & 16 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,26 +286,15 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
data_struct
.fields
.iter()
.enumerate()
// The outmost handler belongs to the last behaviour.
.rev()
.enumerate()
.map(|(enum_n, (field_n, field))| {
let handler = if field_n == 0 {
// Given that the iterator is reversed, this is the innermost handler only.
quote! { let handler = handlers }
} else {
quote! {
let (handlers, handler) = handlers.into_inner()
}
};
.map(|(enum_n, field)| {
let inject = match field.ident {
Some(ref i) => quote! {
self.#i.on_swarm_event(#from_swarm::ConnectionClosed(#connection_closed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));
},
Expand All @@ -314,14 +303,12 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));
},
};

quote! {
#handler;
#inject;
}
})
Expand Down Expand Up @@ -842,7 +829,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
std::task::Poll::Pending
}

fn on_swarm_event(&mut self, event: #from_swarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: #from_swarm) {
match event {
#from_swarm::ConnectionEstablished(
#connection_established { peer_id, connection_id, endpoint, failed_addresses, other_established })
Expand All @@ -851,7 +838,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
#address_change { peer_id, connection_id, old, new })
=> { #(#on_address_change_stmts)* }
#from_swarm::ConnectionClosed(
#connection_closed { peer_id, connection_id, endpoint, handler: handlers, remaining_established })
#connection_closed { peer_id, connection_id, endpoint, remaining_established })
=> { #(#on_connection_closed_stmts)* }
#from_swarm::DialFailure(
#dial_failure { peer_id, connection_id, error })
Expand Down
5 changes: 5 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@

- Add ability to `downcast_ref` ConnectionDenied errors. See [PR 4020].

- Remove `handler` field from `ConnectionClosed`.
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
If you need to transfer state from a `ConnectionHandler` to its `NetworkBehaviour` when a connection closes, use `ConnectionHandler::poll_close`.
See [PR 4076.

[PR 3292]: https://github.com/libp2p/rust-libp2p/pull/3292
[PR 3605]: https://github.com/libp2p/rust-libp2p/pull/3605
[PR 3651]: https://github.com/libp2p/rust-libp2p/pull/3651
Expand All @@ -84,6 +88,7 @@
[PR 3956]: https://github.com/libp2p/rust-libp2p/pull/3956
[PR 4020]: https://github.com/libp2p/rust-libp2p/pull/4020
[PR 4037]: https://github.com/libp2p/rust-libp2p/pull/4037
[PR 4076]: https://github.com/libp2p/rust-libp2p/pull/4076

## 0.42.2

Expand Down
Loading