Struct TimeoutBody
pub struct TimeoutBody<B> { /* private fields */ }
Expand description
Middleware that applies a timeout to request and response bodies.
Wrapper around a Body
to time out if data is not ready within the specified duration.
The timeout is enforced between consecutive Frame
polls, and it
resets after each poll.
The total time to produce a Body
could exceed the timeout duration without
timing out, as long as no single interval between polls exceeds the timeout.
If the Body
does not produce a requested data frame within the timeout period, it will return a TimeoutError
.
§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.
§Example
use rama_core::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§
§impl<B> TimeoutBody<B>
impl<B> TimeoutBody<B>
pub fn new(timeout: Duration, body: B) -> TimeoutBody<B>
pub fn new(timeout: Duration, body: B) -> TimeoutBody<B>
Creates a new TimeoutBody
.
Trait Implementations§
§impl<B> Body for TimeoutBody<B>
impl<B> Body for TimeoutBody<B>
§fn poll_frame(
self: Pin<&mut TimeoutBody<B>>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<<TimeoutBody<B> as Body>::Data>, <TimeoutBody<B> as Body>::Error>>>
fn poll_frame( self: Pin<&mut TimeoutBody<B>>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Frame<<TimeoutBody<B> as Body>::Data>, <TimeoutBody<B> as Body>::Error>>>
§fn is_end_stream(&self) -> bool
fn is_end_stream(&self) -> bool
true
when the end of stream has been reached. Read moreimpl<'__pin, B> Unpin for TimeoutBody<B>where
<PinnedFieldsOfHelperStruct<__Origin<'__pin, B>> as PinnedFieldsOfHelperTrait>::Actual: 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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§impl<T> BodyExt for T
impl<T> BodyExt for T
§fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
Frame
, if any.§fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
§fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
Collected
body which will collect all the DATA frames
and trailers.§fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
§fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§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> ⓘ
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> ⓘ
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more