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

fix(request-response): don't keep duplicate addresses #4700

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions protocols/request-response/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- Remove `request_response::Config::set_connection_keep_alive` in favor of `SwarmBuilder::idle_connection_timeout`.
See [PR 4679](https://github.com/libp2p/rust-libp2p/pull/4679).

- Keep peer addresses in `HashSet` instead of `SmallVec` to prevent adding duplicate addresses.
See [PR 4700](https://github.com/libp2p/rust-libp2p/pull/4700).

## 0.25.2

- Deprecate `request_response::Config::set_connection_keep_alive` in favor of `SwarmBuilder::idle_connection_timeout`.
Expand Down
11 changes: 7 additions & 4 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ where
/// reachable addresses, if any.
connected: HashMap<PeerId, SmallVec<[Connection; 2]>>,
/// Externally managed addresses via `add_address` and `remove_address`.
addresses: HashMap<PeerId, SmallVec<[Multiaddr; 6]>>,
addresses: HashMap<PeerId, HashSet<Multiaddr>>,
/// Requests that have not yet been sent and are waiting for a connection
/// to be established.
pending_outbound_requests: HashMap<PeerId, SmallVec<[RequestProtocol<TCodec>; 10]>>,
Expand Down Expand Up @@ -437,8 +437,11 @@ where
/// by [`NetworkBehaviour::handle_pending_outbound_connection`].
///
/// Addresses added in this way are only removed by `remove_address`.
pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) {
self.addresses.entry(*peer).or_default().push(address);
///
/// Returns true if the address was added, false otherwise (i.e. if the
/// address is already in the list).
pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) -> bool {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
self.addresses.entry(*peer).or_default().insert(address)
}

/// Removes an address of a peer previously added via `add_address`.
Expand Down Expand Up @@ -731,7 +734,7 @@ where
addresses.extend(connections.iter().filter_map(|c| c.remote_address.clone()))
}
if let Some(more) = self.addresses.get(&peer) {
addresses.extend(more.into_iter().cloned());
addresses.extend(more.iter().cloned());
}

Ok(addresses)
Expand Down