Function lambda_runtime::run_with_streaming_response
source · pub async fn run_with_streaming_response<A, B, F>(
handler: F
) -> Result<(), Error>where
F: Service<LambdaEvent<A>>,
F::Future: Future<Output = Result<Response<B>, F::Error>>,
F::Error: Debug + Display,
A: for<'de> Deserialize<'de>,
B: HttpBody + Unpin + Send + 'static,
B::Data: Into<Bytes> + Send,
B::Error: Into<Error> + Send + Debug,
Expand description
Starts the Lambda Rust runtime and stream response back Configure Lambda Streaming Response.
Example
use hyper::{body::Body, Response};
use lambda_runtime::{service_fn, Error, LambdaEvent};
use std::{thread, time::Duration};
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<(), Error> {
lambda_runtime::run_with_streaming_response(service_fn(func)).await?;
Ok(())
}
async fn func(_event: LambdaEvent<Value>) -> Result<Response<Body>, Error> {
let messages = vec!["Hello ", "world ", "from ", "Lambda!"];
let (mut tx, rx) = Body::channel();
tokio::spawn(async move {
for message in messages.iter() {
tx.send_data((*message).into()).await.unwrap();
thread::sleep(Duration::from_millis(500));
}
});
let resp = Response::builder()
.header("content-type", "text/plain")
.header("CustomHeader", "outerspace")
.body(rx)?;
Ok(resp)
}