More pipelined reads

This commit is contained in:
Dominik Werder
2024-10-30 16:12:53 +01:00
parent eb49ee9296
commit b0d9d5f0a8
9 changed files with 359 additions and 245 deletions

View File

@@ -157,13 +157,20 @@ impl IntoBody for ToJsonBody {
pub fn body_stream<S, I, E>(stream: S) -> StreamBody
where
S: Stream<Item = Result<I, E>> + Send + 'static,
I: Into<Bytes>,
E: fmt::Display,
I: Into<Bytes> + Send,
E: fmt::Display + Send,
{
let stream = stream.map(|x| match x {
Ok(x) => Ok(Frame::data(x.into())),
Err(_e) => Err(BodyError::Bad),
});
let stream = stream
.inspect(|x| {
if let Err(e) = x {
error!("observe error in body stream: {e}");
}
})
.take_while(|x| futures_util::future::ready(x.is_ok()))
.map(|x| match x {
Ok(x) => Ok(Frame::data(x.into())),
Err(_e) => Err(BodyError::Bad),
});
StreamBody::new(Box::pin(stream))
}