Skip to content

Commit

Permalink
feat: mark dns address connected time on peer store
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jan 21, 2025
1 parent b44d2d2 commit 3c5e11a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
24 changes: 18 additions & 6 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct NetworkState {
pub(crate) peer_store: Mutex<PeerStore>,
/// Node listened addresses
pub(crate) listened_addrs: RwLock<Vec<Multiaddr>>,
dialing_addrs: RwLock<HashMap<PeerId, Instant>>,
dialing_addrs: RwLock<HashMap<PeerId, (Instant, Multiaddr)>>,
/// Node public addresses,
/// includes manually public addrs and remote peer observed addrs
public_addrs: RwLock<HashSet<Multiaddr>>,
Expand Down Expand Up @@ -405,7 +405,7 @@ impl NetworkState {
return false;
}

if let Some(dial_started) = self.dialing_addrs.read().get(peer_id) {
if let Some((dial_started, _)) = self.dialing_addrs.read().get(peer_id) {
trace!(
"Do not send repeated dial commands to network service: {:?}, {}",
peer_id,
Expand Down Expand Up @@ -435,9 +435,11 @@ impl NetworkState {
true
}

pub(crate) fn dial_success(&self, addr: &Multiaddr) {
pub(crate) fn dial_success(&self, addr: &Multiaddr) -> Option<Multiaddr> {
if let Some(peer_id) = extract_peer_id(addr) {
self.dialing_addrs.write().remove(&peer_id);
self.dialing_addrs.write().remove(&peer_id).map(|a| a.1)
} else {
None
}
}

Expand Down Expand Up @@ -467,7 +469,7 @@ impl NetworkState {
p2p_control.dial(addr.clone(), target)?;
self.dialing_addrs.write().insert(
extract_peer_id(&addr).expect("verified addr"),
Instant::now(),
(Instant::now(), addr),
);
Ok(())
}
Expand Down Expand Up @@ -721,7 +723,17 @@ impl ServiceHandle for EventHandler {
"SessionOpen({}, {})",
session_context.id, session_context.address,
);
self.network_state.dial_success(&session_context.address);
// marked dns address connected message
if let Some(addr) = self.network_state.dial_success(&session_context.address) {
let has_dns = addr
.iter()
.any(|p| matches!(p, Protocol::Dns4(_) | Protocol::Dns6(_)));

if has_dns {
self.network_state
.with_peer_store_mut(|p| p.update_outbound_addr_last_connected_ms(addr))
}
}

let iter = self.inbound_eviction();

Expand Down
16 changes: 15 additions & 1 deletion network/src/peer_store/peer_store_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
Flags, PeerId, SessionType,
};
use ipnetwork::IpNetwork;
use p2p::multiaddr::Protocol;
use rand::prelude::IteratorRandom;
use std::collections::{hash_map::Entry, HashMap};

Expand Down Expand Up @@ -110,7 +111,20 @@ impl PeerStore {
if self.ban_list.is_addr_banned(&addr) {
return;
}
if let Some(info) = self.addr_manager.get_mut(&addr) {
let base_addr = addr
.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_)
) {
None
} else {
Some(p)
}
})
.collect();
if let Some(info) = self.addr_manager.get_mut(&base_addr) {
info.last_connected_at_ms = ckb_systemtime::unix_time_as_millis()
}
}
Expand Down

0 comments on commit 3c5e11a

Please sign in to comment.