Struct lambda_runtime::Runtime

source ·
pub struct Runtime<S> { /* private fields */ }
Expand description

Lambda runtime executing a handler function on incoming requests.

Middleware can be added to a runtime using the Runtime::layer method in order to execute logic prior to processing the incoming request and/or after the response has been sent back to the Lambda Runtime API.

§Example

use lambda_runtime::{Error, LambdaEvent, Runtime};
use serde_json::Value;
use tower::service_fn;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let func = service_fn(func);
    Runtime::new(func).run().await?;
    Ok(())
}

async fn func(event: LambdaEvent<Value>) -> Result<Value, Error> {
    Ok(event.payload)
}

Implementations§

source§

impl<'a, F, EventPayload, Response, BufferedResponse, StreamingResponse, StreamItem, StreamError> Runtime<RuntimeApiClientService<RuntimeApiResponseService<CatchPanicService<'a, F>, EventPayload, Response, BufferedResponse, StreamingResponse, StreamItem, StreamError>>>
where F: Service<LambdaEvent<EventPayload>, Response = Response>, F::Future: Future<Output = Result<Response, F::Error>>, F::Error: Into<Diagnostic> + Debug, EventPayload: for<'de> Deserialize<'de>, Response: IntoFunctionResponse<BufferedResponse, StreamingResponse>, BufferedResponse: Serialize, StreamingResponse: Stream<Item = Result<StreamItem, StreamError>> + Unpin + Send + 'static, StreamItem: Into<Bytes> + Send, StreamError: Into<BoxError> + Send + Debug,

source

pub fn new(handler: F) -> Self

Create a new runtime that executes the provided handler for incoming requests.

In order to start the runtime and poll for events on the Lambda Runtime APIs, you must call Runtime::run.

Note that manually creating a Runtime does not add tracing to the executed handler as is done by super::run. If you want to add the default tracing functionality, call Runtime::layer with a super::layers::TracingLayer.

source§

impl<S> Runtime<S>

source

pub fn layer<L>(self, layer: L) -> Runtime<L::Service>
where L: Layer<S>, L::Service: Service<LambdaInvocation, Response = (), Error = BoxError>,

Add a new layer to this runtime. For an incoming request, this layer will be executed before any layer that has been added prior.

§Example
use lambda_runtime::{layers, Error, LambdaEvent, Runtime};
use serde_json::Value;
use tower::service_fn;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let runtime = Runtime::new(service_fn(echo)).layer(
        layers::TracingLayer::new()
    );
    runtime.run().await?;
    Ok(())
}

async fn echo(event: LambdaEvent<Value>) -> Result<Value, Error> {
    Ok(event.payload)
}
source§

impl<S> Runtime<S>
where S: Service<LambdaInvocation, Response = (), Error = BoxError>,

source

pub async fn run(self) -> Result<(), BoxError>

Start the runtime and begin polling for events on the Lambda Runtime API.

Auto Trait Implementations§

§

impl<S> Freeze for Runtime<S>
where S: Freeze,

§

impl<S> !RefUnwindSafe for Runtime<S>

§

impl<S> Send for Runtime<S>
where S: Send,

§

impl<S> Sync for Runtime<S>
where S: Sync,

§

impl<S> Unpin for Runtime<S>
where S: Unpin,

§

impl<S> !UnwindSafe for Runtime<S>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more