use std::future::Future;
use std::pin::Pin;
use std::task::{Context as TaskContext, Poll};
use bytes::Bytes;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
pub use tokio_util::sync::CancellationToken;
use crate::{Deadline, RuntimeError};
pub type Chunk = Result<Bytes, RuntimeError>;
#[derive(Debug)]
pub struct Producer {
sender: mpsc::Sender<Chunk>,
cancel: CancellationToken,
deadline: Deadline,
}
impl Producer {
pub async fn send(&self, chunk: impl Into<Bytes>) -> bool {
self.sender.send(Ok(chunk.into())).await.is_ok()
}
pub async fn fail(&self, error: RuntimeError) -> bool {
self.sender.send(Err(error)).await.is_ok()
}
pub fn should_stop(&self) -> bool {
self.cancel.is_cancelled() || self.deadline.is_expired() || self.sender.is_closed()
}
pub async fn cancelled(&self) {
self.cancel.cancelled().await;
}
pub fn deadline(&self) -> Deadline {
self.deadline
}
}
#[derive(Debug)]
pub struct StreamBody {
receiver: mpsc::Receiver<Chunk>,
producer: Option<JoinHandle<Result<(), RuntimeError>>>,
cancel: CancellationToken,
finished: bool,
}
impl StreamBody {
pub fn spawn<F, Fut>(capacity: usize, deadline: Deadline, produce: F) -> Self
where
F: FnOnce(Producer) -> Fut + Send + 'static,
Fut: std::future::Future<Output = ()> + Send + 'static,
{
let (sender, receiver) = mpsc::channel(capacity.max(1));
let cancel = CancellationToken::new();
let stopped = cancel.clone();
let work = async move {
let started = std::time::Instant::now();
let work = produce(Producer {
sender,
cancel: stopped.clone(),
deadline,
});
tokio::pin!(work);
let expired = tokio::select! {
biased;
() = stopped.cancelled() => false,
() = tokio::time::sleep(deadline.remaining()) => true,
() = &mut work => return if deadline.is_expired() {
Err(RuntimeError::DeadlineExceeded { elapsed: started.elapsed() })
} else {
Ok(())
},
};
stopped.cancel();
let _ = tokio::time::timeout(std::time::Duration::from_millis(50), &mut work).await;
if expired {
Err(RuntimeError::DeadlineExceeded {
elapsed: started.elapsed(),
})
} else {
Ok(())
}
};
#[cfg(feature = "logs")]
let work = tracing::Instrument::in_current_span(work);
let handle = tokio::spawn(work);
Self {
receiver,
producer: Some(handle),
cancel,
finished: false,
}
}
#[must_use]
pub fn once(chunk: Option<Bytes>) -> Self {
let (sender, receiver) = mpsc::channel(1);
if let Some(chunk) = chunk {
let _ = sender.try_send(Ok(chunk));
}
Self {
receiver,
producer: None,
cancel: CancellationToken::new(),
finished: false,
}
}
pub async fn shutdown(mut self) {
self.cancel.cancel();
if let Some(handle) = self.producer.take() {
let _ = handle.await;
}
}
}
impl futures_util::Stream for StreamBody {
type Item = Chunk;
fn poll_next(
mut self: Pin<&mut Self>,
context: &mut TaskContext<'_>,
) -> Poll<Option<Self::Item>> {
if self.finished {
return Poll::Ready(None);
}
match self.receiver.poll_recv(context) {
Poll::Ready(Some(Ok(chunk))) => Poll::Ready(Some(Ok(chunk))),
Poll::Ready(Some(Err(error))) => {
self.finished = true;
self.cancel.cancel();
self.receiver.close();
Poll::Ready(Some(Err(error)))
}
Poll::Ready(None) => {
let result = match self.producer.as_mut() {
Some(handle) => match Pin::new(handle).poll(context) {
Poll::Pending => return Poll::Pending,
Poll::Ready(result) => result,
},
None => return Poll::Ready(None),
};
self.finished = true;
match result {
Ok(Ok(())) => Poll::Ready(None),
Ok(Err(error)) => Poll::Ready(Some(Err(error))),
Err(_) => Poll::Ready(Some(Err(RuntimeError::message(
"the stream producer stopped unexpectedly",
)))),
}
}
Poll::Pending => Poll::Pending,
}
}
}
impl Drop for StreamBody {
fn drop(&mut self) {
self.cancel.cancel();
}
}
pub async fn run<App, In, H, F>(app: std::sync::Arc<App>, handler: H) -> Result<(), RuntimeError>
where
App: Send + Sync + 'static,
In: serde::de::DeserializeOwned + Send,
H: Fn(std::sync::Arc<App>, In, crate::Context<()>) -> F + Send + Sync,
F: std::future::Future<Output = (lambda_runtime::MetadataPrelude, StreamBody)> + Send,
{
let handler = &handler;
let service = lambda_runtime::service_fn(move |event: lambda_runtime::LambdaEvent<In>| {
let app = std::sync::Arc::clone(&app);
async move {
let (payload, context) = event.into_parts();
let invocation = crate::runtime::invocation_from(&context);
let deadline = invocation
.deadline
.with_margin(std::time::Duration::from_millis(100));
#[cfg(feature = "logs")]
let span = crate::telemetry::logs::invocation_span("streaming", &invocation);
let work = handler(app, payload, crate::Context::new(invocation, ()));
#[cfg(feature = "logs")]
let work = tracing::Instrument::instrument(work, span);
let (metadata_prelude, stream) = deadline
.run(work)
.await
.map_err(lambda_runtime::Error::from)?;
Ok::<_, lambda_runtime::Error>(lambda_runtime::StreamResponse {
metadata_prelude,
stream,
})
}
});
lambda_runtime::Runtime::new(service)
.run()
.await
.map_err(crate::runtime::loop_failure)
}