2022-03-29 20:59:17 +00:00
|
|
|
use actix_web::web::Bytes;
|
2023-08-23 16:59:42 +00:00
|
|
|
use futures_core::Stream;
|
2023-09-10 22:55:13 +00:00
|
|
|
use std::{pin::Pin, time::Duration};
|
|
|
|
use streem::IntoStreamer;
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-21 00:46:10 +00:00
|
|
|
use crate::future::WithMetrics;
|
|
|
|
|
|
|
|
pub(crate) fn metrics<S>(name: &'static str, stream: S) -> impl Stream<Item = S::Item>
|
|
|
|
where
|
|
|
|
S: Stream,
|
|
|
|
S::Item: 'static,
|
|
|
|
{
|
|
|
|
streem::from_fn(|yielder| {
|
|
|
|
async move {
|
|
|
|
let stream = std::pin::pin!(stream);
|
|
|
|
let mut streamer = stream.into_streamer();
|
|
|
|
|
|
|
|
while let Some(item) = streamer.next().await {
|
|
|
|
yielder.yield_(item).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.with_metrics(name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn make_send<S>(stream: S) -> impl Stream<Item = S::Item> + Send
|
2023-09-06 01:45:07 +00:00
|
|
|
where
|
2023-09-10 22:55:13 +00:00
|
|
|
S: Stream + 'static,
|
|
|
|
S::Item: Send + Sync,
|
2023-09-06 01:45:07 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
let (tx, rx) = crate::sync::channel(1);
|
2023-09-06 01:45:07 +00:00
|
|
|
|
2023-10-21 00:08:11 +00:00
|
|
|
let handle = crate::sync::spawn("send-stream", async move {
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = std::pin::pin!(stream);
|
|
|
|
let mut streamer = stream.into_streamer();
|
2023-09-06 01:45:07 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
while let Some(res) = streamer.next().await {
|
|
|
|
if tx.send_async(res).await.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
2023-09-06 01:45:07 +00:00
|
|
|
}
|
2023-09-10 22:55:13 +00:00
|
|
|
});
|
2023-09-06 01:45:07 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::from_fn(|yiedler| async move {
|
|
|
|
let mut stream = rx.into_stream().into_streamer();
|
2023-09-06 01:45:07 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
while let Some(res) = stream.next().await {
|
|
|
|
yiedler.yield_(res).await;
|
2023-09-06 01:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let _ = handle.await;
|
|
|
|
})
|
2023-08-24 00:10:10 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn from_iterator<I>(iterator: I, buffer: usize) -> impl Stream<Item = I::Item> + Send
|
2023-08-24 00:10:10 +00:00
|
|
|
where
|
2023-09-10 22:55:13 +00:00
|
|
|
I: IntoIterator + Send + 'static,
|
|
|
|
I::Item: Send + Sync,
|
2023-08-24 00:10:10 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
let (tx, rx) = crate::sync::channel(buffer);
|
2023-08-24 00:10:10 +00:00
|
|
|
|
2023-10-21 00:08:11 +00:00
|
|
|
let handle = crate::sync::spawn_blocking("blocking-iterator", move || {
|
2023-09-10 22:55:13 +00:00
|
|
|
for value in iterator {
|
|
|
|
if tx.send(value).is_err() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-08-23 16:59:42 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::from_fn(|yielder| async move {
|
|
|
|
let mut stream = rx.into_stream().into_streamer();
|
2023-08-23 16:59:42 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
while let Some(res) = stream.next().await {
|
|
|
|
yielder.yield_(res).await;
|
|
|
|
}
|
2023-08-23 16:59:42 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let _ = handle.await;
|
|
|
|
})
|
2023-08-23 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 03:51:07 +00:00
|
|
|
pub(crate) fn map<S, I1, I2, F>(stream: S, f: F) -> impl Stream<Item = I2>
|
2023-08-23 16:59:42 +00:00
|
|
|
where
|
2023-09-11 03:51:07 +00:00
|
|
|
S: Stream<Item = I1>,
|
|
|
|
I2: 'static,
|
|
|
|
F: Fn(I1) -> I2 + Copy,
|
2023-08-23 16:59:42 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::from_fn(|yielder| async move {
|
|
|
|
let stream = std::pin::pin!(stream);
|
|
|
|
let mut streamer = stream.into_streamer();
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
while let Some(res) = streamer.next().await {
|
2023-09-11 03:51:07 +00:00
|
|
|
yielder.yield_((f)(res)).await;
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
2023-09-10 22:55:13 +00:00
|
|
|
})
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 03:51:07 +00:00
|
|
|
#[cfg(not(feature = "io-uring"))]
|
|
|
|
pub(crate) fn map_ok<S, T1, T2, E, F>(stream: S, f: F) -> impl Stream<Item = Result<T2, E>>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Result<T1, E>>,
|
|
|
|
T2: 'static,
|
|
|
|
E: 'static,
|
|
|
|
F: Fn(T1) -> T2 + Copy,
|
|
|
|
{
|
|
|
|
map(stream, move |res| res.map(f))
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn map_err<S, T, E1, E2, F>(stream: S, f: F) -> impl Stream<Item = Result<T, E2>>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Result<T, E1>>,
|
|
|
|
T: 'static,
|
|
|
|
E2: 'static,
|
|
|
|
F: Fn(E1) -> E2 + Copy,
|
|
|
|
{
|
2023-09-11 03:51:07 +00:00
|
|
|
map(stream, move |res| res.map_err(f))
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn from_err<S, T, E1, E2>(stream: S) -> impl Stream<Item = Result<T, E2>>
|
2023-09-04 02:30:47 +00:00
|
|
|
where
|
2023-09-10 22:55:13 +00:00
|
|
|
S: Stream<Item = Result<T, E1>>,
|
2023-09-04 02:30:47 +00:00
|
|
|
T: 'static,
|
2023-09-10 22:55:13 +00:00
|
|
|
E1: Into<E2>,
|
|
|
|
E2: 'static,
|
2023-09-04 02:30:47 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
map_err(stream, Into::into)
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn empty<T>() -> impl Stream<Item = T>
|
2023-09-04 02:30:47 +00:00
|
|
|
where
|
|
|
|
T: 'static,
|
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::from_fn(|_| std::future::ready(()))
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn once<T>(value: T) -> impl Stream<Item = T>
|
2022-03-29 20:59:17 +00:00
|
|
|
where
|
2023-09-10 22:55:13 +00:00
|
|
|
T: 'static,
|
2022-03-29 20:59:17 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::from_fn(|yielder| yielder.yield_(value))
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn timeout<S>(
|
|
|
|
duration: Duration,
|
|
|
|
stream: S,
|
|
|
|
) -> impl Stream<Item = Result<S::Item, TimeoutError>>
|
2022-03-29 20:59:17 +00:00
|
|
|
where
|
2023-09-10 22:55:13 +00:00
|
|
|
S: Stream,
|
|
|
|
S::Item: 'static,
|
2022-03-29 20:59:17 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::try_from_fn(|yielder| async move {
|
2023-10-21 00:08:11 +00:00
|
|
|
tokio::time::timeout(duration, async move {
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = std::pin::pin!(stream);
|
|
|
|
let mut streamer = stream.into_streamer();
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
while let Some(res) = streamer.next().await {
|
|
|
|
yielder.yield_ok(res).await;
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
2023-09-10 22:55:13 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map_err(|_| TimeoutError)
|
|
|
|
})
|
2022-03-29 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) fn limit<S, E>(limit: usize, stream: S) -> impl Stream<Item = Result<Bytes, E>>
|
2022-03-29 20:59:17 +00:00
|
|
|
where
|
2023-09-10 22:55:13 +00:00
|
|
|
S: Stream<Item = Result<Bytes, E>>,
|
|
|
|
E: From<LimitError> + 'static,
|
2022-03-29 20:59:17 +00:00
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
streem::try_from_fn(|yielder| async move {
|
|
|
|
let stream = std::pin::pin!(stream);
|
|
|
|
let mut streamer = stream.into_streamer();
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let mut count = 0;
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
while let Some(bytes) = streamer.try_next().await? {
|
|
|
|
count += bytes.len();
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
if count > limit {
|
|
|
|
return Err(LimitError.into());
|
|
|
|
}
|
2022-03-29 20:59:17 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
yielder.yield_ok(bytes).await;
|
|
|
|
}
|
2022-03-29 21:18:00 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
2022-03-29 21:18:00 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
pub(crate) type LocalBoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + 'a>>;
|
2022-03-29 21:18:00 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error("Resonse body larger than size limit")]
|
|
|
|
pub(crate) struct LimitError;
|
2022-03-29 21:18:00 +00:00
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error("Timeout in body")]
|
|
|
|
pub(crate) struct TimeoutError;
|