I have this application that uses a black-boxed piece of functionality. 98% of the time, this black-boxed functionality runs very fast as expected. Sometimes though, for reasons I cannot yet debug, this function takes so long that the page times out. Is there a way to make sure that just this block of code only executes for X number of seconds?
Ok, my last attempt at answering this question was a total failure that demonstrated my lack of understanding regarding the CFLock tag. Thankfully, Christoph Schmitz has shown my the light. I have re-attempted an answer using the CFSetting / RequestTimeOut tag and attribute. I am not 100% sure it can be done this way, but testing seems to say that Yes, it can.
<!--- Try to execute the following block of code. ---> <cftry> <!--- By setting a request time out for this page, we are forcing it to have a maximum amount of time that it can run. Since the black-boxed code comes after it, it will be dominated by this CFSetting. ---> <cfsetting requesttimeout="5" /> <!--- This is our black-boxed piece of code. Not sure how it works, but we know that 2% of the time it runs way longer than it should and crashes the page. ---> <p> This will only be allowed to execute for a maximum of 5 seconds. </p> <!--- Catch an errors that get thrown from our code exceeding the CFSetting timeout attribute. ---> <cfcatch> <!--- Our black-boxed piece of code has run too long. Use this opprotunity to set default values so that you might be able to recover from this timeout. ---> <!--- Since the rest of the page will take some time to execute, set a new time out for this request. ---> <cfsetting requesttimeout="60" /> <!--- Perform recovery code here. ---> </cfcatch> </cftry>
Hopefully this is better.
Want to use code from this post? Check out the license.