Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions src/proto/streams/counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ impl Counts {
}
}

/// Accounts for DATA that was discarded without buffering.
pub fn record_discarded_data_frame(
&mut self,
payload_len: usize,
) -> Result<(), BudgetExhausted> {
// Empty frames still count toward their own limit, and large frames
// still replenish the budget. Small non-empty frames need no charge
// because their framing overhead is no longer held in memory.
if payload_len == 0 || payload_len >= DEFAULT_DATA_FRAME_OVERHEAD_THRESHOLD {
self.record_data_frame(payload_len)?;
}
Ok(())
}

/// Returns true when the next opened stream will reach capacity of outbound streams
///
/// The number of client send streams is incremented in prioritize; send_request has to guess if
Expand Down
7 changes: 5 additions & 2 deletions src/proto/streams/recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) enum Open {

pub(super) enum DataFrameAccounting {
Charge,
Discarded,
UseEmptyDataAllowance,
}

Expand Down Expand Up @@ -702,7 +703,9 @@ impl Recv {
"recv_data; frame ignored on locally reset {:?} for some time",
stream.id,
);
return self.ignore_data(sz).map(|()| DataFrameAccounting::Charge);
return self
.ignore_data(sz)
.map(|()| DataFrameAccounting::Discarded);
}

// Ensure that there is enough capacity on the connection before acting
Expand Down Expand Up @@ -754,7 +757,7 @@ impl Recv {
stream.id,
);
self.release_connection_capacity(sz, &mut None);
return Ok(DataFrameAccounting::Charge);
return Ok(DataFrameAccounting::Discarded);
}

// Update stream level flow control
Expand Down
11 changes: 9 additions & 2 deletions src/proto/streams/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,15 @@ impl Inner {
.and_then(|accounting| {
// Final DATA frames and empty frames using the stream's allowance
// are exempt from the connection-wide accounting.
if !is_end_stream && matches!(accounting, DataFrameAccounting::Charge) {
counts.record_data_frame(payload_len).map_err(|_| {
if !is_end_stream {
match accounting {
DataFrameAccounting::Charge => counts.record_data_frame(payload_len),
DataFrameAccounting::Discarded => {
counts.record_discarded_data_frame(payload_len)
}
DataFrameAccounting::UseEmptyDataAllowance => Ok(()),
}
.map_err(|_| {
tracing::debug!("too many small DATA frames");
Error::library_go_away_data(
Reason::ENHANCE_YOUR_CALM,
Expand Down
37 changes: 37 additions & 0 deletions tests/h2-tests/tests/stream_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,43 @@ async fn too_many_small_data_frames_sends_goaway() {
join(mock, h2).await;
}

#[tokio::test]
async fn discarded_small_data_frame_does_not_use_budget() {
h2_support::trace_init!();

let (io, mut srv) = mock::new();

let mock = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("GET", "https://example.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.recv_frame(frames::reset(1).cancel()).await;
srv.send_frame(frames::data(1, "a")).await;
srv.ping_pong([1; 8]).await;
};

let h2 = async move {
// No budget is needed for DATA that never enters the receive buffer.
let (mut client, mut h2) = client::Builder::new()
.data_frame_budget(0)
.handshake::<_, Bytes>(io)
.await
.unwrap();
let response = h2.drive(client.get("https://example.com/")).await.unwrap();
drop(response);
h2.await.unwrap();
drop(client);
};

join(mock, h2).await;
}

#[tokio::test]
async fn many_small_final_data_frames_do_not_exhaust_budget() {
h2_support::trace_init!();
Expand Down