Skip to content

Commit

Permalink
Clear stats every update interval; don't log short requests
Browse files Browse the repository at this point in the history
  • Loading branch information
int08h committed Feb 15, 2024
1 parent 297af1f commit cce4059
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::kms::KmsError;
use crate::tag::Tag;

/// Error types generated by this implementation
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Error {
/// The associated tag was added to an `RtMessage` in non-increasing order.
TagNotStrictlyIncreasing(Tag),
Expand All @@ -32,8 +32,8 @@ pub enum Error {
/// Tag value length exceeds length of source bytes
InvalidValueLength(Tag, u32),

/// Encoding failed. The associated `std::io::Error` should provide more information.
EncodingFailure(std::io::Error),
/// Encoding failed. The associated String should provide more information.
EncodingFailure(String),

/// Request was less than 1024 bytes
RequestTooShort,
Expand Down Expand Up @@ -65,7 +65,7 @@ pub enum Error {

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::EncodingFailure(err)
Error::EncodingFailure(err.to_string())
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use mio::net::{TcpListener, UdpSocket};
use mio_extras::timer::Timer;

use crate::config::ServerConfig;
use crate::Error::RequestTooShort;
use crate::key::LongTermKey;
use crate::kms;
use crate::request;
Expand Down Expand Up @@ -216,8 +217,12 @@ impl Server {
Err(e) => {
self.stats.add_invalid_request(&src_addr.ip());

info!(
"Invalid request: '{:?}' ({} bytes) from {} (#{} in batch)",
// No need to log spammy short packets
if e == RequestTooShort {
continue
}

info!("Invalid request: '{:?}' ({} bytes) from {} (#{} in batch)",
e, num_bytes, src_addr, i
);
}
Expand Down Expand Up @@ -301,6 +306,7 @@ impl Server {
self.stats.total_failed_send_attempts()
);

self.stats.clear();
self.timer.set_timeout(self.status_interval, ());
}
}
11 changes: 10 additions & 1 deletion src/stats/aggregated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,14 @@ impl ServerStats for AggregatedStats {
self.empty_map.iter()
}

fn clear(&mut self) {}
fn clear(&mut self) {
self.rfc_requests = 0;
self.classic_requests = 0;
self.invalid_requests = 0;
self.health_checks = 0;
self.rfc_responses_sent = 0;
self.classic_responses_sent = 0;
self.bytes_sent = 0;
self.failed_send_attempts = 0;
}
}
3 changes: 1 addition & 2 deletions src/stats/per_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ impl Default for PerClientStats {
}
}

/// Maximum number of entries to prevent DoS and unbounded memory growth.
pub const MAX_CLIENTS: usize = 100_000;

impl PerClientStats {
/// Maximum number of entries to prevent DoS and unbounded memory growth.
pub fn new() -> Self {
PerClientStats {
clients: HashMap::with_capacity(MAX_CLIENTS),
Expand Down

0 comments on commit cce4059

Please sign in to comment.