pub struct TimeoutBody<B> { /* private fields */ }Expand description
Middleware that applies a timeout to request and response bodies.
Wrapper around a http_body::Body to time out if data is not ready within the specified duration.
Bodies must produce data at most within the specified timeout. If the body does not produce a requested data frame within the timeout period, it will return an error.
§Differences from [Timeout][crate::timeout::Timeout]
[Timeout][crate::timeout::Timeout] applies a timeout to the request future, not body.
That timeout is not reset when bytes are handled, whether the request is active or not.
Bodies are handled asynchronously outside of the tower stack’s future and thus needs an additional timeout.
This middleware will return a TimeoutError.
§Example
use bytes::Bytes;
use std::time::Duration;
use rama_http::{Request, Response};
use rama_http::dep::http_body_util::Full;
use rama_http::layer::timeout::RequestBodyTimeoutLayer;
use rama_core::{Layer, service::service_fn};
async fn handle(_: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, std::convert::Infallible> {
// ...
}
let svc = RequestBodyTimeoutLayer::new(
// Timeout bodies after 30 seconds of inactivity
Duration::from_secs(30)
).layer(service_fn(handle));Implementations§
Source§impl<B> TimeoutBody<B>
impl<B> TimeoutBody<B>
Sourcepub fn new(timeout: Duration, body: B) -> Self
pub fn new(timeout: Duration, body: B) -> Self
Creates a new TimeoutBody.
Trait Implementations§
Source§impl<B> Body for TimeoutBody<B>
impl<B> Body for TimeoutBody<B>
impl<'__pin, B> Unpin for TimeoutBody<B>where
PinnedFieldsOf<__Origin<'__pin, B>>: Unpin,
Auto Trait Implementations§
impl<B> !Freeze for TimeoutBody<B>
impl<B> !RefUnwindSafe for TimeoutBody<B>
impl<B> Send for TimeoutBody<B>where
B: Send,
impl<B> Sync for TimeoutBody<B>where
B: Sync,
impl<B> !UnwindSafe for TimeoutBody<B>
Blanket Implementations§
Source§impl<T> BodyExt for T
impl<T> BodyExt for T
Source§fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
Returns a future that resolves to the next
Frame, if any.Source§fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
Maps this body’s frame to a different kind.
Source§fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
Maps this body’s error value to a different value.
Source§fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
Turn this body into a boxed trait object that is !Sync.
Source§fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
Turn this body into
Collected body which will collect all the DATA frames
and trailers.Source§fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
Add trailers to the body. Read more
Source§fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
Turn this body into
BodyDataStream.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more