Skip to content

Commit

Permalink
Simplify the router last ping state
Browse files Browse the repository at this point in the history
  • Loading branch information
tiram88 committed Apr 25, 2023
1 parent 3c86a0a commit 0490040
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
6 changes: 3 additions & 3 deletions protocol/flows/src/v5/ping.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{flow_context::FlowContext, flow_trait::Flow};
use kaspa_core::debug;
use kaspa_core::{debug, time::unix_now};
use kaspa_p2p_lib::{
common::ProtocolError,
dequeue, dequeue_with_timeout, make_message,
Expand Down Expand Up @@ -93,8 +93,8 @@ impl SendPingsFlow {
// Create a fresh random nonce for each ping
let nonce = rand::thread_rng().gen::<u64>();
let ping = make_message!(Payload::Ping, PingMessage { nonce });
let ping_time = unix_now();
if let Some(router) = self.router.upgrade() {
router.set_ping_pending(nonce);
router.enqueue(ping).await?;
} else {
return Err(ProtocolError::ConnectionClosed);
Expand All @@ -106,7 +106,7 @@ impl SendPingsFlow {
debug!("Successful ping with peer {} (nonce: {})", self.peer, pong.nonce);
}
if let Some(router) = self.router.upgrade() {
router.set_ping_idle();
router.set_last_ping_duration(unix_now() - ping_time);
} else {
return Err(ProtocolError::ConnectionClosed);
}
Expand Down
22 changes: 3 additions & 19 deletions protocol/p2p/src/core/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::core::hub::HubEvent;
use crate::pb::KaspadMessage;
use crate::Peer;
use crate::{common::ProtocolError, KaspadMessagePayloadType};
use kaspa_core::time::unix_now;
use kaspa_core::{debug, error, info, trace};
use kaspa_utils::networking::PeerId;
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -51,12 +50,6 @@ struct RouterMutableState {
/// Properties of the peer
properties: Arc<PeerProperties>,

/// The nonce of the last ping we sent
last_ping_nonce: u64,

/// Time last ping was sent
last_ping_time: u64,

/// Duration of the last ping to this peer
last_ping_duration: u64,
}
Expand Down Expand Up @@ -231,18 +224,9 @@ impl Router {
self.mutable_state.lock().properties = properties;
}

/// Sets the ping state of the peer to 'pending'
pub fn set_ping_pending(&self, nonce: u64) {
let mut state = self.mutable_state.lock();
state.last_ping_nonce = nonce;
state.last_ping_time = unix_now();
}

/// sets the ping state of the peer to 'idle'
pub fn set_ping_idle(&self) {
let mut state = self.mutable_state.lock();
state.last_ping_nonce = 0;
state.last_ping_duration = unix_now() - state.last_ping_time;
/// Sets the duration of the last ping
pub fn set_last_ping_duration(&self, last_ping_duration: u64) {
self.mutable_state.lock().last_ping_duration = last_ping_duration;
}

pub fn last_ping_duration(&self) -> u64 {
Expand Down

0 comments on commit 0490040

Please sign in to comment.