Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with:

Using The Timeout Attribute With CFHttp In ColdFusion To Limit Blocking

By Ben Nadel on
Tags: ColdFusion

By default, when you make a CFHttp request in ColdFusion, the tag will block the current page request until the CFHttp tag returns a response. Most of the time, this is what you want. But, if you're dealing with a non-critical request (such as a 3rd-party "event" API), it can often make sense to prevent as much blocking as possible. In such cases, you can use the Timeout attribute of the CFHttp tag to minimize the amount of time the calling context will wait.

The Timeout attribute will let you go as low as one second. And, by default, when / if the CFHttp tag does timeout, ColdFusion will suppress the error. If you want to force the error, you can use the throwOnError attribute; but, that would defeat the purpose of this post.

This approach has been awesome and I've been using it for years with much success. But, to be honest, I don't think I've ever actually looked at the CFHttp response for a timed-out request. As such, I wanted to put together a quick demo to showcase the difference between a successfully completed request and a timed-out request:

  • <cfscript>
  •  
  • // Build the common arguments to be used across both HTTP request.
  • // I'm doing this just to emphasize that the only thing changing
  • // in the second request is the timeout-related arguments.
  • httpArguments = {
  • method = "get",
  • url = "http://#cgi.server_name##getDirectoryFromPath( cgi.script_name )#long-task.cfm"
  • };
  •  
  •  
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  •  
  •  
  • // Block the current page request until the HTTP request returns.
  • apiRequest = new Http( argumentCollection = httpArguments );
  •  
  • writeDump(
  • var = apiRequest.send().getPrefix(),
  • label = "No Timeout"
  • );
  •  
  •  
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  •  
  •  
  • // Block the current page for a maximum of 1-second, even if the
  • // HTTP request has not returned.
  • apiRequest = new Http(
  • argumentCollection = httpArguments,
  • timeout = 1
  • );
  •  
  • writeDump(
  • var = apiRequest.send().getPrefix(),
  • label = "1-Second Timeout"
  • );
  •  
  • </cfscript>

The URL that these CFHttp requests are hitting has a sleep() command for 3-seconds. As such, the first request will complete successfully and the second request - with a 1-second timeout - will stop blocking before the CFHttp tag has a chance to finish. When we run the above code, we get the following page output:


 
 
 

 
 Using Timeout with CFHttp in ColdFusion. 
 
 
 

As you can see, the CFHttp tag that timed-out presents with a 408 Request Time-out response.

Again, this is an interesting fact, but it's not critical; after all, we used the Timeout attribute because we don't care about the HTTP response. That said, it's nice to know what it looks like in case we have additional logic in our code that inspects HTTP status codes.

You may be looking at this demo and thinking to yourself, "Just put the CFHttp tag inside a CFThread tag." To which I would agree. But, even once you get your CFHttp inside of a CFThread tag, you'd still want the Timeout attribute in place. Without it, the CFHttp request will block the thread and (potentially) consume your limited asynchronous thread resources. At the end of the day, even if you don't care about your CFHttp response, you still want to make sure it consumes as few resources as possible.




Reader Comments

Thanks for sharing this! I use cfhttp a lot each day and has most of them are threaded, I do need to know if a call timed out so the data can be reviewed and pushed back through.

Reply to this Comment

Just the other day, I had a CFFeed instance that was running (according to FusionReactor) for like 100-thousand seconds. The target site wouldn't response. This morning, I threw a "timeout" attribute on the CFFeed tag as well.

Reply to this Comment

I think this question is related but not sure... if not please accept my apologies...

I have a google map that populates to my site, but suffice it to say, that when there is no internet connection, the app workflow falls flat on its face. Is there a way to code the page so that if there is no internet connection, I can still recover gracefully, that is, -to isolate the map service processing apart from the rest of the page?
(This will also help me greatly when developing the app offline)
Or is there a way to test to see if there is a www network connection first and if not provide an alternate less happy path??

I just thought of rendering the map to a seperate template and cfincluding it into the view page.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.