Skip to content

Commit

Permalink
fix: even more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
piot committed Apr 12, 2024
1 parent 1b77892 commit bc72b17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
19 changes: 14 additions & 5 deletions crates/session/src/connection_quality.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use std::time::Instant;
use log::trace;

use crate::metrics::RateMetrics;

Expand All @@ -17,16 +17,25 @@ pub enum QualityAssessment {
pub struct ConnectionQuality {
pub last_ping_at: Instant,
pub pings_per_second: RateMetrics,
pub last_pings_per_second: f32,
pub assessment: QualityAssessment,
threshold: f32,
}


impl fmt::Display for ConnectionQuality {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[quality pings_per_second:{} assessment: {:?}]", self.last_pings_per_second, self.assessment)
}
}

impl ConnectionQuality {
pub fn new(threshold: f32, time: Instant) -> Self {
Self {
assessment: QualityAssessment::NeedMoreInformation,
last_ping_at: Instant::now(),
pings_per_second: RateMetrics::new(time),
last_pings_per_second: 0.0,
threshold,
}
}
Expand All @@ -40,15 +49,15 @@ impl ConnectionQuality {
if !self.pings_per_second.has_enough_time_passed(time) {
self.assessment = QualityAssessment::NeedMoreInformation;
} else {
let pings_per_second = self.pings_per_second.calculate_rate(time);
self.assessment = if pings_per_second < self.threshold {
self.last_pings_per_second = self.pings_per_second.calculate_rate(time);
self.assessment = if self.last_pings_per_second < self.threshold {
QualityAssessment::RecommendDisconnect
} else if pings_per_second > self.threshold * 2.0 {
} else if self.last_pings_per_second > self.threshold * 2.0 {
QualityAssessment::Good
} else {
QualityAssessment::Acceptable
};
trace!("pings_per_second {}, assessment {:?}", pings_per_second, self.assessment);

}
}
}
9 changes: 6 additions & 3 deletions crates/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//! Evaluating connection quality for all connections attached to the room. Using "votes" from the connections, together with
//! [Knowledge] and [ConnectionQuality] it determines which connection should be appointed leader.

extern crate core;

use core::fmt;
use std::collections::HashMap;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -64,7 +66,7 @@ pub struct Connection {

impl fmt::Display for Connection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[conn id:{} knowledge:{} connectedToHost:{:?} knownTerm:{:?}]", self.id, self.knowledge, self.has_connection_host, self.last_reported_term)
write!(f, "[conn id:{}, knowledge:{}, connectedToHost:{:?}, knownTerm:{:?}, quality:{}]", self.id, self.knowledge, self.has_connection_host, self.last_reported_term, self.quality)
}
}

Expand Down Expand Up @@ -99,6 +101,7 @@ impl Connection {

fn update(&mut self, time: Instant) {
self.quality.update(time);
trace!("update {}", self);
}

pub fn assessment(&self) -> QualityAssessment {
Expand Down Expand Up @@ -417,10 +420,10 @@ impl Room {

#[cfg(test)]
mod tests {
use test_log::test;

use std::time::{Duration, Instant};

use test_log::test;

use conclave_types::{ConnectionToLeader, Knowledge, Term};

use crate::{QualityAssessment, Room, RoomConfig};
Expand Down

0 comments on commit bc72b17

Please sign in to comment.