Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Caching data between invocations #372

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

Closed
rimutaka opened this issue Nov 21, 2021 · 1 comment
Closed

Caching data between invocations #372

rimutaka opened this issue Nov 21, 2021 · 1 comment

Comments

@rimutaka
Copy link
Contributor

It is possible to initialize a resource in main and pass it onto a closure inside handler_fn as a shared reference. Example: https://github.com/awslabs/aws-lambda-rust-runtime/blob/master/lambda-runtime/examples/shared_resource.rs

The downside of this method is that the initialization must be done upfront and the resource cannot be updated.

I've been trying to implement a cache that can be updated from inside the handler, but no matter what I tried it's a breaking change.

Does anyone know a way of updating a shared resource from inside the handler fn without breaking the current handler_fn interface?

@softprops
Copy link
Contributor

hander_fn is a factory method for creating an instance of a type which implements a generic Handler trait.

The run function accepts any type which implements Handler so I believe you can just have a type with state that lives across invocations of a lambda task that implements Handler

example pseudo code

struct Counter {
  data: usize
} 

impl Default for Counter {
  fn default() -> Self {
     Counter { data: 0 }
  }
} 

impl Handler<A, B> for Counter {
 type Error = YourErrType;
 type Fut = YourFutType;
 fn call(&mut self, req: A, ctx: Context) -> Self::Fut {
    // update your state
    self.data += 1;
    // do the thing that returns Self::Fut
 }   
}

fn main() {
  lambda_runtime::run(Counter::default()).await?;
  Ok(())
}

@awslabs awslabs locked and limited conversation to collaborators Jan 10, 2022
@calavera calavera converted this issue into discussion #397 Jan 10, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants