Does Lambda support CancellationToken? #1835
-
I've noticed I implemented a lot of async code (services, repositories, etc.) with cancellation token support, but I'm actually not sure if Lambda supports this. Looking for an answer to help me decide if I should remove Does |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you need a cancellation token to pass around, you'll need to implement a CancellationSource in the handler method which will be based on the |
Beta Was this translation helpful? Give feedback.
-
No, AspNetCore lambda doesn't know whether request was aborted from client's side. |
Beta Was this translation helpful? Give feedback.
If you need a cancellation token to pass around, you'll need to implement a CancellationSource in the handler method which will be based on the
LambdaContext.RemainingTime
. Once that time runs out, AWS will kill the lambda, so you need to subtract enough time to gracefully cancel. They have an example here.I've implemented an async job as well following the onion architecture (service-provider-repo) and the jobs could run <1 second or upwards of the entire time. I use a cancellation source that subtracts 2 minutes from the remaining time just to be safe and will adjust as needed.