feat: configurable rate_limit for the ping utility

This commit is contained in:
skkeye 2025-02-09 23:16:14 -05:00 committed by Ulf Lilleengen
parent bdb1b81213
commit 6d7a5f949c

View File

@ -407,8 +407,8 @@ pub mod ping {
// 1 sec min per ping // 1 sec min per ping
let rate_limit_end = rate_limit_start.elapsed(); let rate_limit_end = rate_limit_start.elapsed();
if rate_limit_end <= Duration::from_secs(1) { if rate_limit_end <= params.rate_limit {
Timer::after(Duration::from_secs(1).checked_sub(rate_limit_end).unwrap()).await; Timer::after(params.rate_limit.checked_sub(rate_limit_end).unwrap()).await;
} }
} }
// calculate and return the average duration // calculate and return the average duration
@ -579,6 +579,7 @@ pub mod ping {
/// * `hop_limit` - The hop limit to be used by the socket. /// * `hop_limit` - The hop limit to be used by the socket.
/// * `count` - The number of pings to be sent in one ping operation. /// * `count` - The number of pings to be sent in one ping operation.
/// * `timeout` - The timeout duration before returning a [`PingError::DestinationHostUnreachable`] error. /// * `timeout` - The timeout duration before returning a [`PingError::DestinationHostUnreachable`] error.
/// * `rate_limit` - The minimum time per echo request
pub struct PingParams<'a> { pub struct PingParams<'a> {
target: Option<IpAddr>, target: Option<IpAddr>,
#[cfg(feature = "proto-ipv6")] #[cfg(feature = "proto-ipv6")]
@ -587,6 +588,7 @@ pub mod ping {
hop_limit: Option<u8>, hop_limit: Option<u8>,
count: u16, count: u16,
timeout: Duration, timeout: Duration,
rate_limit: Duration,
} }
impl Default for PingParams<'_> { impl Default for PingParams<'_> {
@ -599,6 +601,7 @@ pub mod ping {
hop_limit: None, hop_limit: None,
count: 4, count: 4,
timeout: Duration::from_secs(4), timeout: Duration::from_secs(4),
rate_limit: Duration::from_secs(1),
} }
} }
} }
@ -614,6 +617,7 @@ pub mod ping {
hop_limit: None, hop_limit: None,
count: 4, count: 4,
timeout: Duration::from_secs(4), timeout: Duration::from_secs(4),
rate_limit: Duration::from_secs(1),
} }
} }
@ -701,5 +705,16 @@ pub mod ping {
pub fn timeout(&self) -> Duration { pub fn timeout(&self) -> Duration {
self.timeout self.timeout
} }
/// Sets the `rate_limit`: minimum time per echo request
pub fn set_rate_limit(&mut self, rate_limit: Duration) -> &mut Self {
self.rate_limit = rate_limit;
self
}
/// Retrieve the rate_limit
pub fn rate_limit(&self) -> Duration {
self.rate_limit
}
} }
} }