Skip to content

Caching data between invocations #372

Closed

Description

@rimutaka

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?

Activity

  1. softprops commented on Nov 22, 2021

    @softprops
    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(())
    }
  2. locked and limited conversation to collaborators on Jan 10, 2022
  3. converted this issue into a discussion #397 on Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions