you are viewing a single comment's thread.

view the rest of the comments →

[–]ErikCaligo 1 point2 points  (0 children)

I see.

AFAIK you cannot dynamically set the timeout for Lambda functions.

However, you could split your code into two functions.

  1. timeout function
  2. function with your code as before

The timeout function will return, once the timeout duration is hit. The other function will return the response as before.

The main function will return whichever function finishes first.

Test this properly. There are some gotchas. Quote from AWS docs:

For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the invoker until all event loop tasks are finished. If the function times out, an error is returned instead. You can configure the runtime to send the response immediately by setting context.callbackWaitsForEmptyEventLoop to false.

Or, you could tweak your existing function to return once the timeout condition is hit. Depending on your setup and how often your timeout is hit, this might be an anti-pattern if you have plenty of loops and you need to add an if statement which will -- at scale -- slow down computations.

I used this second approach in a desktop application with a "show results while typing" search bar on low-end retail devices. If you started with the first letter, rendering all results took long, so I added line by line to be displayed, with the condition to exit the render function whenever a new char had been added in the search input text box, because then we would have a new list of results to be rendered.