Skip to content

Support IDisposable for function handlers #131

Closed

Description

@bugsancho

Hi,

I currently have a lambda handler class that has multiple entry points, each with its own lambda function.

The issue that I'm currently facing is the need to do some cleanup code after each invocation of any of the lambdas, so I tried to implement IDisposable on my function handler class, but it seems that is it never invoked, so currently the only option is to manually wrap all my code in try-finally clauses which is a bit messy.

In my current case I need to ensure NLog flushes all the logs to the console (ergo CloudWatch), but I imagine there can be many similar use cases for doing cleanup logic after each request.

Any suggestions would be highly appreciated!

Thanks!

Activity

  1. normj commented on Jun 15, 2017

    @normj
    Member

    The instance of the class containing the Lambda functions will stay around for the lifetime of the Lambda compute environment. This is great for maintaining cached state between function innovation but in your case that does mean for your case the class won't be disposed after an invocation.

    Your options are like you said wrap each function in a try finally or put your functions in a separate class in then in the exposed Lambda function create that class in a using block and then invoke the real implementation. Here is a rough sketch of what I mean.

    public class Function
    {
       public void LambdaInterface1(string content, ILambdaContext context)
       {
            using(var logic = new MyLogic())
            {
                 logic.RealLogic1(content, context);
            }
       }
    
       public void LambdaInterface2(string content, ILambdaContext context)
       {
            using(var logic = new MyLogic())
            {
                 logic.RealLogic2(content, context);
            }
       }
    }
    
    public class MyLogic : IDisposable
    {
    
       public void RealLogic1(string content, ILambdaContext context)
       {
              ....
       }
       
       public void RealLogic2(string content, ILambdaContext context)
       {
              ....
       }
    
       public void Dispose()
       {
              ....
       }
    }
  2. normj commented on Jun 22, 2017

    @normj
    Member

    Closing as I believe this is answered. If not feel free to reopen.

  3. added
    guidanceQuestion that needs advice or information.
    and removed on Jan 3, 2019
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

    guidanceQuestion that needs advice or information.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions