Struct h2::share::FlowControl

source ·
pub struct FlowControl {
    inner: OpaqueStreamRef,
}
Expand description

A handle to release window capacity to a remote stream.

This type allows the caller to manage inbound data flow control. The caller is expected to call release_capacity after dropping data frames.

§Overview

Each stream has a window size. This window size is the maximum amount of inbound data that can be in-flight. In-flight data is defined as data that has been received, but not yet released.

When a stream is created, the window size is set to the connection’s initial window size value. When a data frame is received, the window size is then decremented by size of the data frame before the data is provided to the caller. As the caller finishes using the data, release_capacity must be called. This will then increment the window size again, allowing the peer to send more data.

There is also a connection level window as well as the stream level window. Received data counts against the connection level window as well and calls to release_capacity will also increment the connection level window.

§Sending WINDOW_UPDATE frames

WINDOW_UPDATE frames will not be sent out for every call to release_capacity, as this would end up slowing down the protocol. Instead, h2 waits until the window size is increased to a certain threshold and then sends out a single WINDOW_UPDATE frame representing all the calls to release_capacity since the last WINDOW_UPDATE frame.

This essentially batches window updating.

§Scenarios

Following is a basic scenario with an HTTP/2 connection containing a single active stream.

  • A new stream is activated. The receive window is initialized to 1024 (the value of the initial window size for this connection).
  • A DATA frame is received containing a payload of 600 bytes.
  • The receive window size is reduced to 424 bytes.
  • release_capacity is called with 200.
  • The receive window size is now 624 bytes. The peer may send no more than this.
  • A DATA frame is received with a payload of 624 bytes.
  • The window size is now 0 bytes. The peer may not send any more data.
  • release_capacity is called with 1024.
  • The receive window size is now 1024 bytes. The peer may now send more data.

Fields§

§inner: OpaqueStreamRef

Implementations§

source§

impl FlowControl

source

pub(crate) fn new(inner: OpaqueStreamRef) -> Self

source

pub fn stream_id(&self) -> StreamId

Returns the stream ID of the stream whose capacity will be released by this FlowControl.

source

pub fn available_capacity(&self) -> isize

Get the current available capacity of data this stream could receive.

source

pub fn used_capacity(&self) -> usize

Get the currently used capacity for this stream.

This is the amount of bytes that can be released back to the remote.

source

pub fn release_capacity(&mut self, sz: usize) -> Result<(), Error>

Release window capacity back to remote stream.

This releases capacity back to the stream level and the connection level windows. Both window sizes will be increased by sz.

See struct level documentation for more details.

§Errors

This function errors if increasing the receive window size by sz would result in a window size greater than the target window size. In other words, the caller cannot release more capacity than data has been received. If 1024 bytes of data have been received, at most 1024 bytes can be released.

Trait Implementations§

source§

impl Clone for FlowControl

source§

fn clone(&self) -> FlowControl

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for FlowControl

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more