Skip to content

Commit

Permalink
Record the number of send retries in server
Browse files Browse the repository at this point in the history
  • Loading branch information
int08h committed May 28, 2024
1 parent bd083d6 commit 2cf049f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Responder {

let mut bytes_sent: usize = 0;
let mut successful_send: bool = true;
match self.send_with_retry(socket, &src_addr, &resp_bytes) {
match self.send_with_retry(socket, &src_addr, &resp_bytes, stats) {
Ok(num_bytes) => bytes_sent = num_bytes,
Err(_) => successful_send = false,
}
Expand Down Expand Up @@ -154,6 +154,7 @@ impl Responder {
socket: &mut UdpSocket,
src_addr: &SocketAddr,
resp_bytes: &Vec<u8>,
stats: &mut Box<dyn ServerStats>,
) -> Result<usize, Error> {
let mut retries_remaining = 2;

Expand All @@ -162,6 +163,7 @@ impl Responder {
Ok(bytes_sent) => return Ok(bytes_sent),
Err(_) => {} // no-op
}
stats.add_retried_send_attempt(&src_addr.ip());
retries_remaining -= 1;
}

Expand Down
21 changes: 16 additions & 5 deletions src/stats/aggregated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ pub struct AggregatedStats {
rfc_responses_sent: u64,
classic_responses_sent: u64,
bytes_sent: usize,
failed_send_attempts: u64,
send_failed_attempts: u64,
send_retry_attempts: u64,
empty_map: HashMap<IpAddr, ClientStatEntry>,
}

Expand All @@ -53,7 +54,8 @@ impl AggregatedStats {
rfc_responses_sent: 0,
classic_responses_sent: 0,
bytes_sent: 0,
failed_send_attempts: 0,
send_failed_attempts: 0,
send_retry_attempts: 0,
empty_map: HashMap::new(),
}
}
Expand All @@ -73,7 +75,11 @@ impl ServerStats for AggregatedStats {
}

fn add_failed_send_attempt(&mut self, _: &IpAddr) {
self.failed_send_attempts += 1;
self.send_failed_attempts += 1;
}

fn add_retried_send_attempt(&mut self, _: &IpAddr) {
self.send_retry_attempts += 1;
}

fn add_health_check(&mut self, _: &IpAddr) {
Expand Down Expand Up @@ -111,7 +117,11 @@ impl ServerStats for AggregatedStats {
}

fn total_failed_send_attempts(&self) -> u64 {
self.failed_send_attempts
self.send_failed_attempts
}

fn total_retried_send_attempts(&self) -> u64 {
self.send_retry_attempts
}

fn total_responses_sent(&self) -> u64 {
Expand Down Expand Up @@ -150,6 +160,7 @@ impl ServerStats for AggregatedStats {
self.rfc_responses_sent = 0;
self.classic_responses_sent = 0;
self.bytes_sent = 0;
self.failed_send_attempts = 0;
self.send_failed_attempts = 0;
self.send_retry_attempts = 0;
}
}
8 changes: 8 additions & 0 deletions src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct ClientStatEntry {
pub classic_responses_sent: u64,
pub bytes_sent: usize,
pub failed_send_attempts: u64,
pub retried_send_attempts: u64,
}

impl ClientStatEntry {
Expand All @@ -51,6 +52,7 @@ impl ClientStatEntry {
classic_responses_sent: 0,
bytes_sent: 0,
failed_send_attempts: 0,
retried_send_attempts: 0,
}
}
}
Expand All @@ -67,6 +69,8 @@ pub trait ServerStats {

fn add_failed_send_attempt(&mut self, addr: &IpAddr);

fn add_retried_send_attempt(&mut self, addr: &IpAddr);

fn add_health_check(&mut self, addr: &IpAddr);

fn add_rfc_response(&mut self, addr: &IpAddr, bytes_sent: usize);
Expand All @@ -85,6 +89,8 @@ pub trait ServerStats {

fn total_failed_send_attempts(&self) -> u64;

fn total_retried_send_attempts(&self) -> u64;

fn total_responses_sent(&self) -> u64;

fn num_rfc_responses_sent(&self) -> u64;
Expand Down Expand Up @@ -119,6 +125,7 @@ mod test {
assert_eq!(stats.total_bytes_sent(), 0);
assert_eq!(stats.total_unique_clients(), 0);
assert_eq!(stats.total_failed_send_attempts(), 0);
assert_eq!(stats.total_retried_send_attempts(), 0);
assert_eq!(stats.num_overflows(), 0);
}

Expand Down Expand Up @@ -162,6 +169,7 @@ mod test {
assert_eq!(entry.classic_responses_sent, 2);
assert_eq!(entry.bytes_sent, 4096);
assert_eq!(entry.failed_send_attempts, 1);
assert_eq!(entry.retried_send_attempts, 0);
}

#[test]
Expand Down
14 changes: 14 additions & 0 deletions src/stats/per_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ impl ServerStats for PerClientStats {
.failed_send_attempts += 1;
}

fn add_retried_send_attempt(&mut self, addr: &IpAddr) {
if self.too_many_entries() {
return;
}
self.clients
.entry(*addr)
.or_insert_with(ClientStatEntry::new)
.retried_send_attempts += 1;
}

fn add_health_check(&mut self, addr: &IpAddr) {
if self.too_many_entries() {
return;
Expand Down Expand Up @@ -181,6 +191,10 @@ impl ServerStats for PerClientStats {
self.clients.values().map(|&v| v.failed_send_attempts).sum()
}

fn total_retried_send_attempts(&self) -> u64 {
self.clients.values().map(|&v| v.retried_send_attempts).sum()
}

fn total_responses_sent(&self) -> u64 {
self.clients
.values()
Expand Down

0 comments on commit 2cf049f

Please sign in to comment.