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?
NOTE: Please disregard the solution below! Christoph Schmitz pointed out that this is a serious misunderstanding of how CFLock timeout works... I thought it was how long it could RUN... it is how long it will WAIT. Sorry for misleading anyone. I thought I was helping other people, but it turns out, this time, I get to learn something :)
Please see a better answer here: http://www.bennadel.com/index.cfm?dax=blog:618.view
For starters, we cannot force a piece of code to execute faster than it is going to on its own. If we could do that, I would make sure all my pages executed in 16ms. What we can do, though, is "misuse" the CFLock tag to make sure that your piece of code is only allowed to execute for X number of seconds before it throws an error. We can leverage the TimeOut attribute of the CFLock tag to allow the code a maximum amount of time in which it can run. If the CFLock times out and throws an error, we can then catch it and try to recover from the too-long-running task:
Launch code in new window » Download code as text file »
There are a few things to notice about the CFLock tag:
I hope this works for you. I would not exactly recommend this technique, though, as locking does come with processing overhead and I am not sure how this page is being used (high traffic?). Furthermore, I believe I read somewhere that repeated calls to CreateUUID() actually start to slow down your machine, but I cannot state that as fact, just hearsay.
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Print Page
Ask Ben: Limiting The Amount Of Time A Block Of Code Can Run (Attempt II)
Project HUGE: Spike'ing My Workout
Ben,
no offense man, but, you got cflock wrong somehow...
The timeout attribute of cflock specifies the time a request will wait until it aquires a lock, it does NOT specify the time a request within the lock may run. You can have a cflock-timeout of 1 second and still have the request run 100s of seconds.
So if you want the cflock to throw an exception, you need to make the lock exclusive and make the lock name static (make the code block single threaded) and then start a second request. Then this second request will timeout after 5 seconds.
IMHO the easiest way to timeout a request is the <cfsetting requesttimeout="x" /> tag.
Chris
Posted by Christoph Schmitz on Apr 2, 2007 at 9:37 AM
@Chris,
Oh man... you are totally right! Thanks for clearing that up for me. That is a huge misunderstanding in my mind. I knew it felt very wrong to use CFLock in this fashion - another reason now, it simply doesn't work that way.
Thanks for showing me the light :)
Posted by Ben Nadel on Apr 2, 2007 at 9:41 AM
@Christoph,
Does this look any better:
http://www.bennadel.com/index.cfm?dax=blog:618.view
Posted by Ben Nadel on Apr 2, 2007 at 10:08 AM