diff --git a/protocols/request-response/CHANGELOG.md b/protocols/request-response/CHANGELOG.md index 872d1320e7e..34dc0704198 100644 --- a/protocols/request-response/CHANGELOG.md +++ b/protocols/request-response/CHANGELOG.md @@ -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`. diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 322d77047f2..db6859dd244 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -326,7 +326,7 @@ where /// reachable addresses, if any. connected: HashMap>, /// Externally managed addresses via `add_address` and `remove_address`. - addresses: HashMap>, + addresses: HashMap>, /// Requests that have not yet been sent and are waiting for a connection /// to be established. pending_outbound_requests: HashMap; 10]>>, @@ -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 { + self.addresses.entry(*peer).or_default().insert(address) } /// Removes an address of a peer previously added via `add_address`. @@ -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)