Skip to content

Commit d7b7aa7

Browse files
authored
Wrap incoming extension events in a struct (#394)
This new structure can be used to pass additional information to the extension in each invocation, like the extension id that the Runtime assigns to the extension when it's initialized. Signed-off-by: David Calavera <[email protected]>
1 parent 7b2c48a commit d7b7aa7

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

lambda-extension/examples/basic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use lambda_extension::{extension_fn, Error, NextEvent};
1+
use lambda_extension::{extension_fn, Error, LambdaEvent, NextEvent};
22

3-
async fn my_extension(event: NextEvent) -> Result<(), Error> {
4-
match event {
3+
async fn my_extension(event: LambdaEvent) -> Result<(), Error> {
4+
match event.next {
55
NextEvent::Shutdown(_e) => {
66
// do something with the shutdown event
77
}

lambda-extension/examples/custom_events.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use lambda_extension::{extension_fn, Error, NextEvent, Runtime};
1+
use lambda_extension::{extension_fn, Error, LambdaEvent, NextEvent, Runtime};
22

3-
async fn my_extension(event: NextEvent) -> Result<(), Error> {
4-
match event {
3+
async fn my_extension(event: LambdaEvent) -> Result<(), Error> {
4+
match event.next {
55
NextEvent::Shutdown(_e) => {
66
// do something with the shutdown event
77
}

lambda-extension/examples/custom_trait_implementation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lambda_extension::{run, Error, Extension, InvokeEvent, NextEvent};
1+
use lambda_extension::{run, Error, Extension, InvokeEvent, LambdaEvent, NextEvent};
22
use std::{
33
future::{ready, Future},
44
pin::Pin,
@@ -11,8 +11,8 @@ struct MyExtension {
1111

1212
impl Extension for MyExtension {
1313
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
14-
fn call(&mut self, event: NextEvent) -> Self::Fut {
15-
match event {
14+
fn call(&mut self, event: LambdaEvent) -> Self::Fut {
15+
match event.next {
1616
NextEvent::Shutdown(_e) => {
1717
self.data.clear();
1818
}

lambda-extension/src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,21 @@ impl NextEvent {
9494
}
9595
}
9696

97+
/// Wrapper with information about the next
98+
/// event that the Lambda Runtime is going to process
99+
pub struct LambdaEvent {
100+
/// ID assigned to this extension by the Lambda Runtime
101+
pub extension_id: String,
102+
/// Next incoming event
103+
pub next: NextEvent,
104+
}
105+
97106
/// A trait describing an asynchronous extension.
98107
pub trait Extension {
99108
/// Response of this Extension.
100109
type Fut: Future<Output = Result<(), Error>>;
101110
/// Handle the incoming event.
102-
fn call(&mut self, event: NextEvent) -> Self::Fut;
111+
fn call(&mut self, event: LambdaEvent) -> Self::Fut;
103112
}
104113

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

120129
impl<F, Fut> Extension for ExtensionFn<F>
121130
where
122-
F: Fn(NextEvent) -> Fut,
131+
F: Fn(LambdaEvent) -> Fut,
123132
Fut: Future<Output = Result<(), Error>>,
124133
{
125134
type Fut = Fut;
126-
fn call(&mut self, event: NextEvent) -> Self::Fut {
135+
fn call(&mut self, event: LambdaEvent) -> Self::Fut {
127136
(self.f)(event)
128137
}
129138
}
@@ -173,6 +182,11 @@ where
173182
let event: NextEvent = serde_json::from_slice(&body)?;
174183
let is_invoke = event.is_invoke();
175184

185+
let event = LambdaEvent {
186+
extension_id: self.extension_id.clone(),
187+
next: event,
188+
};
189+
176190
let res = extension.call(event).await;
177191
if let Err(error) = res {
178192
let req = if is_invoke {

lambda-integration-tests/src/bin/extension-fn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use lambda_extension::{extension_fn, Error, NextEvent};
1+
use lambda_extension::{extension_fn, Error, LambdaEvent, NextEvent};
22
use tracing::info;
33

4-
async fn my_extension(event: NextEvent) -> Result<(), Error> {
5-
match event {
4+
async fn my_extension(event: LambdaEvent) -> Result<(), Error> {
5+
match event.next {
66
NextEvent::Shutdown(e) => {
77
info!("[extension-fn] Shutdown event received: {:?}", e);
88
}

lambda-integration-tests/src/bin/extension-trait.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lambda_extension::{Error, Extension, NextEvent};
1+
use lambda_extension::{Error, Extension, LambdaEvent, NextEvent};
22
use std::{
33
future::{ready, Future},
44
pin::Pin,
@@ -13,8 +13,8 @@ struct MyExtension {
1313
impl Extension for MyExtension {
1414
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
1515

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

0 commit comments

Comments
 (0)