diff --git a/mm2src/mm2_p2p/src/behaviours/atomicdex.rs b/mm2src/mm2_p2p/src/behaviours/atomicdex.rs index 275256f124d..02118f719ae 100644 --- a/mm2src/mm2_p2p/src/behaviours/atomicdex.rs +++ b/mm2src/mm2_p2p/src/behaviours/atomicdex.rs @@ -25,7 +25,7 @@ use std::collections::HashMap; use std::hash::{Hash, Hasher}; use std::iter; use std::net::IpAddr; -use std::sync::Mutex; +use std::sync::{Mutex, MutexGuard}; use std::task::{Context, Poll}; use timed_map::{MapKind, StdClock, TimedMap}; @@ -182,6 +182,22 @@ pub enum AdexBehaviourCmd { }, } +/// Determines if a dial attempt to the remote should be made. +/// +/// Returns `false` if a dial attempt to the given address has already been made, +/// in which case the caller must skip the dial attempt. +fn pre_dial_check(recently_dialed_peers: &mut MutexGuard>, addr: &Multiaddr) -> bool { + if recently_dialed_peers + .insert_expirable(sha256(&addr), (), DIAL_RETRY_DELAY) + .is_some() + { + info!("Connection attempt was already made recently to '{addr}'."); + return false; + } + + true +} + /// Returns info about directly connected peers. pub async fn get_directly_connected_peers(mut cmd_tx: AdexCmdTx) -> HashMap> { let (result_tx, rx) = oneshot::channel(); @@ -771,11 +787,7 @@ fn start_gossipsub( let mut recently_dialed_peers = RECENTLY_DIALED_PEERS.lock().unwrap(); for relay in bootstrap.choose_multiple(&mut rng, mesh_n) { - if recently_dialed_peers - .insert_expirable(sha256(relay), (), DIAL_RETRY_DELAY) - .is_some() - { - info!("Connection attempt was already made recently to '{relay}'. Skipping this dial attempt."); + if !pre_dial_check(&mut recently_dialed_peers, &relay) { continue; } @@ -900,11 +912,7 @@ fn maintain_connection_to_relays(swarm: &mut AtomicDexSwarm, bootstrap_addresses .collect::>() .choose_multiple(&mut rng, connect_bootstrap_num) { - if recently_dialed_peers - .insert_expirable(sha256(addr), (), DIAL_RETRY_DELAY) - .is_some() - { - info!("Connection attempt was already made recently to '{addr}'. Skipping this dial attempt."); + if !pre_dial_check(&mut recently_dialed_peers, &addr) { continue; } @@ -919,11 +927,7 @@ fn maintain_connection_to_relays(swarm: &mut AtomicDexSwarm, bootstrap_addresses continue; } - if recently_dialed_peers - .insert_expirable(sha256(&addr), (), DIAL_RETRY_DELAY) - .is_some() - { - info!("Connection attempt was already made recently to '{addr}'. Skipping this dial attempt."); + if !pre_dial_check(&mut recently_dialed_peers, &addr) { continue; }