From cce405972c46815f3bfd78c60b02ad8f6dab1d6e Mon Sep 17 00:00:00 2001 From: Stuart Stock Date: Thu, 15 Feb 2024 14:32:52 -0600 Subject: [PATCH] Clear stats every update interval; don't log short requests --- src/error.rs | 8 ++++---- src/server.rs | 10 ++++++++-- src/stats/aggregated.rs | 11 ++++++++++- src/stats/per_client.rs | 3 +-- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/error.rs b/src/error.rs index f4dde1f..2aa520f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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), @@ -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, @@ -65,7 +65,7 @@ pub enum Error { impl From for Error { fn from(err: std::io::Error) -> Self { - Error::EncodingFailure(err) + Error::EncodingFailure(err.to_string()) } } diff --git a/src/server.rs b/src/server.rs index d2a3009..dc66172 100644 --- a/src/server.rs +++ b/src/server.rs @@ -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; @@ -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 ); } @@ -301,6 +306,7 @@ impl Server { self.stats.total_failed_send_attempts() ); + self.stats.clear(); self.timer.set_timeout(self.status_interval, ()); } } diff --git a/src/stats/aggregated.rs b/src/stats/aggregated.rs index 3d28136..9f7a940 100644 --- a/src/stats/aggregated.rs +++ b/src/stats/aggregated.rs @@ -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; + } } diff --git a/src/stats/per_client.rs b/src/stats/per_client.rs index a63fa5f..3dcd9fd 100644 --- a/src/stats/per_client.rs +++ b/src/stats/per_client.rs @@ -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),