Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle unstaked quic streams for a given connection #34562

Merged
merged 10 commits into from
Dec 22, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions streamer/src/nonblocking/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use {
tokio::{task::JoinHandle, time::timeout},
};

const MAX_UNSTAKED_STREAMS_PER_CONNECTION_PER_100MS: u64 = 1024;
const WAIT_FOR_STREAM_TIMEOUT: Duration = Duration::from_millis(100);
pub const DEFAULT_WAIT_FOR_CHUNK_TIMEOUT: Duration = Duration::from_secs(10);

Expand Down Expand Up @@ -702,12 +703,33 @@ async fn handle_connection(
);
let stable_id = connection.stable_id();
stats.total_connections.fetch_add(1, Ordering::Relaxed);
let mut next_interval = tokio::time::Instant::now()
pgarg66 marked this conversation as resolved.
Show resolved Hide resolved
.checked_add(Duration::from_millis(100))
.unwrap();
let mut streams_in_current_interval: u64 = 0;
while !stream_exit.load(Ordering::Relaxed) {
if matches!(peer_type, ConnectionPeerType::Unstaked) {
if streams_in_current_interval >= MAX_UNSTAKED_STREAMS_PER_CONNECTION_PER_100MS {
tokio::time::sleep_until(next_interval).await;
pgarg66 marked this conversation as resolved.
Show resolved Hide resolved
}

if next_interval
.saturating_duration_since(tokio::time::Instant::now())
.as_millis()
== 0
{
next_interval = tokio::time::Instant::now()
.checked_add(Duration::from_millis(100))
.unwrap();
streams_in_current_interval = 0;
}
}
if let Ok(stream) =
tokio::time::timeout(WAIT_FOR_STREAM_TIMEOUT, connection.accept_uni()).await
{
match stream {
Ok(mut stream) => {
streams_in_current_interval = streams_in_current_interval.saturating_add(1);
stats.total_streams.fetch_add(1, Ordering::Relaxed);
stats.total_new_streams.fetch_add(1, Ordering::Relaxed);
let stream_exit = stream_exit.clone();
Expand Down