Skip to main content
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Adam Tuttle
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Adam Tuttle

Using The Timeout Attribute With CFHttp In ColdFusion To Limit Blocking

By
Published in Comments (5)

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.

Want to use code from this post? Check out the license.

Reader Comments

3 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.

15,841 Comments

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.

6 Comments

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.

2 Comments

Is there any way to get Timeout time to less then 1 second?
Or even better, for the process that is calling CFHTTP not wait at all for the response? Wrap in cfthread?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel