Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancellation token in Lambda Function Handler C#

Does AWS Lambda Function Handlers in C# provide a cancellation token?

I've read the documentation on AWS site (https://docs.aws.amazon.com/lambda/latest/dg/dotnet-programming-model-handler-types.html) but I can't see anywhere that mentions cancellation tokens. I've also inspected the ILambdaContext that gets passed into the method of execution but there is nothing on there.

I've worked before with Azure Functions and they just pass it in as another argument to the functions as described in this article: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#cancellation-tokens

like image 387
Kevin Smith Avatar asked May 22 '18 17:05

Kevin Smith


People also ask

How do you handle a cancellation token?

The wait handle of the cancellation token will become signaled in response to a cancellation request, and the method can use the return value of the WaitAny method to determine whether it was the cancellation token that signaled. The operation can then just exit, or throw an OperationCanceledException, as appropriate.

What is cancellation token for?

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.

How do I change the handler in Lambda?

This function handler name reflects the function name ( lambda_handler ) and the file where the handler code is stored ( lambda_function.py ). To change the function handler name in the Lambda console, on the Runtime settings pane, choose Edit.

What is ILambdaContext?

ILambdaContext interface can be used in your handler function to access information about the current execution, such as the name of the current function, the memory limit, execution time remaining, and logging. Here is an example of how this interface can be used in your handler function.


1 Answers

The answer is no, as you've discovered. Currently no CancellationToken is provided.

You could make your own using the ILambdaContext.RemainingTime and CancellationTokenSource:

public async Task FunctionHandler(SQSEvent evnt, ILambdaContext context)
{
    var cts = new CancellationTokenSource(context.RemainingTime);
    var myResult = await MyService.DoSomethingAsync(cts.Token);
}

I'm not sure how much good this will do, since when the time remaining is gone the Lambda is frozen, so it's not like your code will have the chance to gracefully stop. Perhaps you can estimate how much time your code needs to gracefully stop and then cancel the token that long before the remaining time, something like:

var gracefulStopTimeLimit = TimeSpan.FromSeconds(2);
var cts = new CancellationTokenSource(context.RemainingTime.Subtract(gracefulStopTimeLimit));
like image 135
Seafish Avatar answered Sep 18 '22 11:09

Seafish