Skip to content

Commit

Permalink
fix: subtract overflow in measure (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 authored Jan 11, 2024
1 parent dd47c2c commit c4715b6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
16 changes: 8 additions & 8 deletions crates/core/src/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ pub trait BehaviourJudgement: Measure {
/// The "goodness" of a node is measured by comparing the connection and disconnection counts against a given threshold.
#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
pub trait ConnectBehaviour<const THRESHOLD: i16>: Measure {
pub trait ConnectBehaviour<const THRESHOLD: i64>: Measure {
/// This asynchronous method returns a boolean indicating whether the node identified by `did` has a satisfactory connection behavior.
async fn good(&self, did: Did) -> bool {
let conn = self.get_count(did, MeasureCounter::Connect).await;
let disconn = self.get_count(did, MeasureCounter::Disconnected).await;
let conn = self.get_count(did, MeasureCounter::Connect).await as i64;
let disconn = self.get_count(did, MeasureCounter::Disconnected).await as i64;
tracing::debug!(
"[ConnectBehaviour] in Threadhold: {:}, connect: {:}, disconn: {:}, delta: {:}",
THRESHOLD,
conn,
disconn,
conn - disconn
);
((conn - disconn) as i16) < THRESHOLD
(conn - disconn) < THRESHOLD
}
}

Expand All @@ -69,11 +69,11 @@ pub trait ConnectBehaviour<const THRESHOLD: i16>: Measure {
/// The "goodness" of a node is measured by comparing the sent and failed-to-send counts against a given threshold.
#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
pub trait MessageSendBehaviour<const THRESHOLD: i16>: Measure {
pub trait MessageSendBehaviour<const THRESHOLD: i64>: Measure {
/// This asynchronous method returns a boolean indicating whether the node identified by `did` has a satisfactory message sending behavior.
async fn good(&self, did: Did) -> bool {
let failed = self.get_count(did, MeasureCounter::FailedToSend).await;
(failed as i16) < THRESHOLD
(failed as i64) < THRESHOLD
}
}

Expand All @@ -82,10 +82,10 @@ pub trait MessageSendBehaviour<const THRESHOLD: i16>: Measure {
/// The "goodness" of a node is measured by comparing the received and failed-to-receive counts against a given threshold.
#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
pub trait MessageRecvBehaviour<const THRESHOLD: i16>: Measure {
pub trait MessageRecvBehaviour<const THRESHOLD: i64>: Measure {
/// This asynchronous method returns a boolean indicating whether the node identified by `did` has a satisfactory message receiving behavior.
async fn good(&self, did: Did) -> bool {
let failed = self.get_count(did, MeasureCounter::FailedToReceive).await;
(failed as i16) < THRESHOLD
(failed as i64) < THRESHOLD
}
}
2 changes: 2 additions & 0 deletions crates/core/src/swarm/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ impl ConnectionManager for Swarm {
impl Judegement for Swarm {
/// Record a succeeded connected
async fn record_connect(&self, did: Did) {
tracing::info!("Record connect {:?}", &did);
if let Some(measure) = &self.measure {
tracing::info!("[Judgement] Record connect");
measure.incr(did, MeasureCounter::Connect).await;
Expand All @@ -409,6 +410,7 @@ impl Judegement for Swarm {

/// Record a disconnected
async fn record_disconnected(&self, did: Did) {
tracing::info!("Record disconnected {:?}", &did);
if let Some(measure) = &self.measure {
tracing::info!("[Judgement] Record disconnected");
measure.incr(did, MeasureCounter::Disconnected).await;
Expand Down
6 changes: 3 additions & 3 deletions crates/derive/src/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ pub fn impl_measure_behaviour_traits(ast: &syn::DeriveInput) -> proc_macro2::Tok
let impl_token = quote! {
#[cfg_attr(feature = "node", async_trait)]
#[cfg_attr(feature = "browser", async_trait(?Send))]
impl<const T: i16> MessageSendBehaviour<T> for #name {}
impl<const T: i64> MessageSendBehaviour<T> for #name {}
#[cfg_attr(feature = "node", async_trait)]
#[cfg_attr(feature = "browser", async_trait(?Send))]
impl<const T: i16> MessageRecvBehaviour<T> for #name {}
impl<const T: i64> MessageRecvBehaviour<T> for #name {}
#[cfg_attr(feature = "node", async_trait)]
#[cfg_attr(feature = "browser", async_trait(?Send))]
impl<const T: i16> ConnectBehaviour<T> for #name {}
impl<const T: i64> ConnectBehaviour<T> for #name {}
};

#[cfg(not(feature = "core_crate"))]
Expand Down
6 changes: 3 additions & 3 deletions crates/node/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ pub const BACKEND_MTU: usize = TRANSPORT_MAX_SIZE - TRANSPORT_MTU;
/// Redundant setting of vnode data storage
pub const DATA_REDUNDANT: u16 = 6;
/// Connect Behaviour
pub const CONNECT_FAILED_LIMIT: i16 = 3;
pub const CONNECT_FAILED_LIMIT: i64 = 3;
/// Message Send Behaviour
pub const MSG_SEND_FAILED_LIMIT: i16 = 10;
pub const MSG_SEND_FAILED_LIMIT: i64 = 10;
/// Message Received Behaviour
pub const MSG_RECV_FAILED_LIMIT: i16 = 10;
pub const MSG_RECV_FAILED_LIMIT: i64 = 10;
/// Timeout for proxied TCP connections
pub const TCP_SERVER_TIMEOUT: u64 = 30;

0 comments on commit c4715b6

Please sign in to comment.