-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport connection stream counter to v1.17 (#991)
* Backport ConnectionStreamCounter * Addressed a feedback from Pankaj
- Loading branch information
1 parent
3a66644
commit 7a02ef7
Showing
3 changed files
with
109 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod quic; | ||
pub mod recvmmsg; | ||
pub mod sendmmsg; | ||
mod stream_throttle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::{ | ||
sync::{ | ||
atomic::{AtomicU64, Ordering}, | ||
RwLock, | ||
}, | ||
time::Duration, | ||
}; | ||
|
||
pub const STREAM_THROTTLING_INTERVAL_MS: u64 = 100; | ||
pub const STREAM_THROTTLING_INTERVAL: Duration = | ||
Duration::from_millis(STREAM_THROTTLING_INTERVAL_MS); | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct ConnectionStreamCounter { | ||
pub(crate) stream_count: AtomicU64, | ||
last_throttling_instant: RwLock<tokio::time::Instant>, | ||
} | ||
|
||
impl ConnectionStreamCounter { | ||
pub(crate) fn new() -> Self { | ||
Self { | ||
stream_count: AtomicU64::default(), | ||
last_throttling_instant: RwLock::new(tokio::time::Instant::now()), | ||
} | ||
} | ||
|
||
/// Reset the counter and last throttling instant and | ||
/// return last_throttling_instant regardless it is reset or not. | ||
pub(crate) fn reset_throttling_params_if_needed(&self) -> tokio::time::Instant { | ||
let last_throttling_instant = *self.last_throttling_instant.read().unwrap(); | ||
if tokio::time::Instant::now().duration_since(last_throttling_instant) | ||
> STREAM_THROTTLING_INTERVAL | ||
{ | ||
let mut last_throttling_instant = self.last_throttling_instant.write().unwrap(); | ||
// Recheck as some other thread might have done throttling since this thread tried to acquire the write lock. | ||
if tokio::time::Instant::now().duration_since(*last_throttling_instant) | ||
> STREAM_THROTTLING_INTERVAL | ||
{ | ||
*last_throttling_instant = tokio::time::Instant::now(); | ||
self.stream_count.store(0, Ordering::Relaxed); | ||
} | ||
*last_throttling_instant | ||
} else { | ||
last_throttling_instant | ||
} | ||
} | ||
} |