Skip to content

Commit

Permalink
fix(request-response): don't keep duplicate addresses
Browse files Browse the repository at this point in the history
Resolves: #4699.

Pull-Request: #4700.
  • Loading branch information
b-zee authored Oct 25, 2023
1 parent 8b3d4e4 commit 7305234
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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 {
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

0 comments on commit 7305234

Please sign in to comment.