Skip to content

Commit 046fa39

Browse files
committed
feat(util): introduce Sender::{capacity, max_capacity}
this commit introduces two methods, allowing callers to inspect the capacity, both available and maximum, of the channel-backed body. this allows callers to observe when the channel is full. Signed-off-by: katelyn martin <me+cratelyn@katelyn.world> X-Ref: #140
1 parent 1090bff commit 046fa39

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

http-body-util/src/channel.rs

+59
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,65 @@ impl<D, E> Sender<D, E> {
9494
self.send(Frame::trailers(trailers)).await
9595
}
9696

97+
/// Returns the current capacity of the channel.
98+
///
99+
/// The capacity goes down when [`Frame<T>`]s are sent. The capacity goes up when these frames
100+
/// are received by the corresponding [`Channel<D, E>`]. This is distinct from
101+
/// [`max_capacity()`][Self::max_capacity], which always returns the buffer capacity initially
102+
/// specified when [`Channel::new()`][Channel::new] was called.
103+
///
104+
/// # Examples
105+
///
106+
/// ```
107+
/// use bytes::Bytes;
108+
/// use http_body_util::{BodyExt, channel::Channel};
109+
/// use std::convert::Infallible;
110+
///
111+
/// #[tokio::main]
112+
/// async fn main() {
113+
/// let (mut tx, mut body) = Channel::<Bytes, Infallible>::new(4);
114+
/// assert_eq!(tx.capacity(), 4);
115+
///
116+
/// // Sending a value decreases the available capacity.
117+
/// tx.send_data(Bytes::from("Hel")).await.unwrap();
118+
/// assert_eq!(tx.capacity(), 3);
119+
///
120+
/// // Reading a value increases the available capacity.
121+
/// let _ = body.frame().await;
122+
/// assert_eq!(tx.capacity(), 4);
123+
/// }
124+
/// ```
125+
pub fn capacity(&mut self) -> usize {
126+
self.tx_frame.capacity()
127+
}
128+
129+
/// Returns the maximum capacity of the channel.
130+
///
131+
/// This function always returns the buffer capacity initially specified when
132+
/// [`Channel::new()`][Channel::new] was called. This is distinct from
133+
/// [`capacity()`][Self::capacity], which returns the currently available capacity.
134+
///
135+
/// # Examples
136+
///
137+
/// ```
138+
/// use bytes::Bytes;
139+
/// use http_body_util::{BodyExt, channel::Channel};
140+
/// use std::convert::Infallible;
141+
///
142+
/// #[tokio::main]
143+
/// async fn main() {
144+
/// let (mut tx, mut body) = Channel::<Bytes, Infallible>::new(4);
145+
/// assert_eq!(tx.max_capacity(), 4);
146+
///
147+
/// // Sending a value buffers it, but does not affect the maximum capacity reported.
148+
/// tx.send_data(Bytes::from("Hel")).await.unwrap();
149+
/// assert_eq!(tx.max_capacity(), 4);
150+
/// }
151+
/// ```
152+
pub fn max_capacity(&mut self) -> usize {
153+
self.tx_frame.max_capacity()
154+
}
155+
97156
/// Aborts the body in an abnormal fashion.
98157
pub fn abort(self, error: E) {
99158
self.tx_error.send(error).ok();

0 commit comments

Comments
 (0)