Handle timeout gracefully?

I have two ways I can resolve a request. I fetch the first request before attempting the alternative method. I check if result of the first one is OK. If it’s not OK I have to go to the second one whether I like it or not. The problem is the second way to resolve my request is very compute-time heavy (worker-limit-wise). If the first one is OK, then I have two choices - I can either return the first one and call it a day, or preferably I would like to try the second one as well. The problem is I’m likely to hit the CPU time limit on the second one.

My question is: can I register a default response in case there was a timeout? In more detail, what I would like to do is: let’s say the first request was successful, so I register a response, like event.passThroughOnTimeout(firstResponse), then if I run out of time on the second one I can still have something to return.

My understanding is that event.passThroughOnException() will hit the origin after a timeout error; however, I only have experience with running a worker like a REST API (without an origin) and not as a proxy, so I’m not quite sure if this would be a good way to solve my issue.

This would also enable me on the second request, given an applicable iterative algorithm, work towards a result of the algorithm, but still be able to return something if I fail. This assumes the algorithm works in a loop and improves the result slowly in each iteration and it’s better to return an un-finished response than no response at all.

1 Like