Skip to content

Wrap incoming extension events in a struct #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lambda-extension/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use lambda_extension::{extension_fn, Error, NextEvent};
use lambda_extension::{extension_fn, Error, LambdaEvent, NextEvent};

async fn my_extension(event: NextEvent) -> Result<(), Error> {
match event {
async fn my_extension(event: LambdaEvent) -> Result<(), Error> {
match event.next {
NextEvent::Shutdown(_e) => {
// do something with the shutdown event
}
Expand Down
6 changes: 3 additions & 3 deletions lambda-extension/examples/custom_events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use lambda_extension::{extension_fn, Error, NextEvent, Runtime};
use lambda_extension::{extension_fn, Error, LambdaEvent, NextEvent, Runtime};

async fn my_extension(event: NextEvent) -> Result<(), Error> {
match event {
async fn my_extension(event: LambdaEvent) -> Result<(), Error> {
match event.next {
NextEvent::Shutdown(_e) => {
// do something with the shutdown event
}
Expand Down
6 changes: 3 additions & 3 deletions lambda-extension/examples/custom_trait_implementation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lambda_extension::{run, Error, Extension, InvokeEvent, NextEvent};
use lambda_extension::{run, Error, Extension, InvokeEvent, LambdaEvent, NextEvent};
use std::{
future::{ready, Future},
pin::Pin,
Expand All @@ -11,8 +11,8 @@ struct MyExtension {

impl Extension for MyExtension {
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn call(&mut self, event: NextEvent) -> Self::Fut {
match event {
fn call(&mut self, event: LambdaEvent) -> Self::Fut {
match event.next {
NextEvent::Shutdown(_e) => {
self.data.clear();
}
Expand Down
20 changes: 17 additions & 3 deletions lambda-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,21 @@ impl NextEvent {
}
}

/// Wrapper with information about the next
/// event that the Lambda Runtime is going to process
pub struct LambdaEvent {
/// ID assigned to this extension by the Lambda Runtime
pub extension_id: String,
/// Next incoming event
pub next: NextEvent,
}

/// A trait describing an asynchronous extension.
pub trait Extension {
/// Response of this Extension.
type Fut: Future<Output = Result<(), Error>>;
/// Handle the incoming event.
fn call(&mut self, event: NextEvent) -> Self::Fut;
fn call(&mut self, event: LambdaEvent) -> Self::Fut;
}

/// Returns a new [`ExtensionFn`] with the given closure.
Expand All @@ -119,11 +128,11 @@ pub struct ExtensionFn<F> {

impl<F, Fut> Extension for ExtensionFn<F>
where
F: Fn(NextEvent) -> Fut,
F: Fn(LambdaEvent) -> Fut,
Fut: Future<Output = Result<(), Error>>,
{
type Fut = Fut;
fn call(&mut self, event: NextEvent) -> Self::Fut {
fn call(&mut self, event: LambdaEvent) -> Self::Fut {
(self.f)(event)
}
}
Expand Down Expand Up @@ -173,6 +182,11 @@ where
let event: NextEvent = serde_json::from_slice(&body)?;
let is_invoke = event.is_invoke();

let event = LambdaEvent {
extension_id: self.extension_id.clone(),
next: event,
};

let res = extension.call(event).await;
if let Err(error) = res {
let req = if is_invoke {
Expand Down
6 changes: 3 additions & 3 deletions lambda-integration-tests/src/bin/extension-fn.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use lambda_extension::{extension_fn, Error, NextEvent};
use lambda_extension::{extension_fn, Error, LambdaEvent, NextEvent};
use tracing::info;

async fn my_extension(event: NextEvent) -> Result<(), Error> {
match event {
async fn my_extension(event: LambdaEvent) -> Result<(), Error> {
match event.next {
NextEvent::Shutdown(e) => {
info!("[extension-fn] Shutdown event received: {:?}", e);
}
Expand Down
6 changes: 3 additions & 3 deletions lambda-integration-tests/src/bin/extension-trait.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lambda_extension::{Error, Extension, NextEvent};
use lambda_extension::{Error, Extension, LambdaEvent, NextEvent};
use std::{
future::{ready, Future},
pin::Pin,
Expand All @@ -13,8 +13,8 @@ struct MyExtension {
impl Extension for MyExtension {
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>;

fn call(&mut self, event: NextEvent) -> Self::Fut {
match event {
fn call(&mut self, event: LambdaEvent) -> Self::Fut {
match event.next {
NextEvent::Shutdown(e) => {
info!("[extension] Shutdown event received: {:?}", e);
}
Expand Down