Skip to content

Commit

Permalink
Fix: Limit the number in the slab
Browse files Browse the repository at this point in the history
  • Loading branch information
silence committed Jun 9, 2022
1 parent e4cf88c commit 2f25ad6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/proto/streams/counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ impl Counts {
self.max_send_streams
}

/// Returns the maximum number of streams.
pub(crate) fn max_streams(&self) -> usize {
self.max_recv_streams + self.max_reset_streams
}

/// Returns the maximum number of streams that can be initiated by the
/// remote peer.
pub(crate) fn max_recv_streams(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/proto/streams/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Store {
self.ids.len()
}

#[cfg(feature = "unstable")]
/// Returns the number of streams that are held in slab.
pub fn num_wired_streams(&self) -> usize {
self.slab.len()
}
Expand Down
17 changes: 16 additions & 1 deletion src/proto/streams/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,14 @@ where
impl<B> DynStreams<'_, B> {
pub fn recv_headers(&mut self, frame: frame::Headers) -> Result<(), Error> {
let mut me = self.inner.lock().unwrap();

if me.store.num_wired_streams() > me.counts.max_streams() {
tracing::error!("HEADERS: number of streams exceeds the upper limit.");
return Err(Error::GoAway(
Bytes::new(),
Reason::PROTOCOL_ERROR,
Initiator::Remote,
));
}
me.recv_headers(self.peer, &self.send_buffer, frame)
}

Expand Down Expand Up @@ -349,6 +356,14 @@ impl<B> DynStreams<'_, B> {

pub fn recv_push_promise(&mut self, frame: frame::PushPromise) -> Result<(), Error> {
let mut me = self.inner.lock().unwrap();
if me.store.num_wired_streams() > me.counts.max_streams() {
tracing::error!("PUSH_PROMISE: number of streams exceeds the upper limit.");
return Err(Error::GoAway(
Bytes::new(),
Reason::PROTOCOL_ERROR,
Initiator::Remote,
));
}
me.recv_push_promise(&self.send_buffer, frame)
}

Expand Down

0 comments on commit 2f25ad6

Please sign in to comment.