Skip to content

Commit

Permalink
fix: deduplicate autonat add_server addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Oct 25, 2023
1 parent e5cee61 commit 3a40389
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,19 @@ 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 {
let addrs = self.addresses.entry(*peer).or_default();

// Add only if address is not already in the list.
if addrs.iter().all(|a| *a != address) {
addrs.push(address);
true
} else {
false
}
}

/// Removes an address of a peer previously added via `add_address`.
Expand Down

0 comments on commit 3a40389

Please sign in to comment.