Skip to content

Commit

Permalink
create function for pre-dial check
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Nov 4, 2024
1 parent 86eb068 commit e86a166
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions mm2src/mm2_p2p/src/behaviours/atomicdex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<TimedMap<StdClock, [u8; 32], ()>>, 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<String, Vec<String>> {
let (result_tx, rx) = oneshot::channel();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -900,11 +912,7 @@ fn maintain_connection_to_relays(swarm: &mut AtomicDexSwarm, bootstrap_addresses
.collect::<Vec<_>>()
.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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit e86a166

Please sign in to comment.