diff options
| author | skkeye <[email protected]> | 2025-02-09 23:16:14 -0500 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2025-02-13 10:27:30 +0100 |
| commit | 6d7a5f949c7a9d3cc5933725baacb075c6af75f1 (patch) | |
| tree | 65a7b7282ee984b6ed58e7af6381cd9269b9bfa8 /embassy-net | |
| parent | bdb1b812135b7cb22f65009242d5b61712e8e9d7 (diff) | |
feat: configurable rate_limit for the ping utility
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/src/icmp.rs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/embassy-net/src/icmp.rs b/embassy-net/src/icmp.rs index ba206a465..dafe6ed8a 100644 --- a/embassy-net/src/icmp.rs +++ b/embassy-net/src/icmp.rs | |||
| @@ -407,8 +407,8 @@ pub mod ping { | |||
| 407 | 407 | ||
| 408 | // 1 sec min per ping | 408 | // 1 sec min per ping |
| 409 | let rate_limit_end = rate_limit_start.elapsed(); | 409 | let rate_limit_end = rate_limit_start.elapsed(); |
| 410 | if rate_limit_end <= Duration::from_secs(1) { | 410 | if rate_limit_end <= params.rate_limit { |
| 411 | Timer::after(Duration::from_secs(1).checked_sub(rate_limit_end).unwrap()).await; | 411 | Timer::after(params.rate_limit.checked_sub(rate_limit_end).unwrap()).await; |
| 412 | } | 412 | } |
| 413 | } | 413 | } |
| 414 | // calculate and return the average duration | 414 | // calculate and return the average duration |
| @@ -579,6 +579,7 @@ pub mod ping { | |||
| 579 | /// * `hop_limit` - The hop limit to be used by the socket. | 579 | /// * `hop_limit` - The hop limit to be used by the socket. |
| 580 | /// * `count` - The number of pings to be sent in one ping operation. | 580 | /// * `count` - The number of pings to be sent in one ping operation. |
| 581 | /// * `timeout` - The timeout duration before returning a [`PingError::DestinationHostUnreachable`] error. | 581 | /// * `timeout` - The timeout duration before returning a [`PingError::DestinationHostUnreachable`] error. |
| 582 | /// * `rate_limit` - The minimum time per echo request | ||
| 582 | pub struct PingParams<'a> { | 583 | pub struct PingParams<'a> { |
| 583 | target: Option<IpAddr>, | 584 | target: Option<IpAddr>, |
| 584 | #[cfg(feature = "proto-ipv6")] | 585 | #[cfg(feature = "proto-ipv6")] |
| @@ -587,6 +588,7 @@ pub mod ping { | |||
| 587 | hop_limit: Option<u8>, | 588 | hop_limit: Option<u8>, |
| 588 | count: u16, | 589 | count: u16, |
| 589 | timeout: Duration, | 590 | timeout: Duration, |
| 591 | rate_limit: Duration, | ||
| 590 | } | 592 | } |
| 591 | 593 | ||
| 592 | impl Default for PingParams<'_> { | 594 | impl Default for PingParams<'_> { |
| @@ -599,6 +601,7 @@ pub mod ping { | |||
| 599 | hop_limit: None, | 601 | hop_limit: None, |
| 600 | count: 4, | 602 | count: 4, |
| 601 | timeout: Duration::from_secs(4), | 603 | timeout: Duration::from_secs(4), |
| 604 | rate_limit: Duration::from_secs(1), | ||
| 602 | } | 605 | } |
| 603 | } | 606 | } |
| 604 | } | 607 | } |
| @@ -614,6 +617,7 @@ pub mod ping { | |||
| 614 | hop_limit: None, | 617 | hop_limit: None, |
| 615 | count: 4, | 618 | count: 4, |
| 616 | timeout: Duration::from_secs(4), | 619 | timeout: Duration::from_secs(4), |
| 620 | rate_limit: Duration::from_secs(1), | ||
| 617 | } | 621 | } |
| 618 | } | 622 | } |
| 619 | 623 | ||
| @@ -701,5 +705,16 @@ pub mod ping { | |||
| 701 | pub fn timeout(&self) -> Duration { | 705 | pub fn timeout(&self) -> Duration { |
| 702 | self.timeout | 706 | self.timeout |
| 703 | } | 707 | } |
| 708 | |||
| 709 | /// Sets the `rate_limit`: minimum time per echo request | ||
| 710 | pub fn set_rate_limit(&mut self, rate_limit: Duration) -> &mut Self { | ||
| 711 | self.rate_limit = rate_limit; | ||
| 712 | self | ||
| 713 | } | ||
| 714 | |||
| 715 | /// Retrieve the rate_limit | ||
| 716 | pub fn rate_limit(&self) -> Duration { | ||
| 717 | self.rate_limit | ||
| 718 | } | ||
| 704 | } | 719 | } |
| 705 | } | 720 | } |
