Maintain Outbound TCP Connection Pool in ColdFusion
Asked Answered
S

2

2

I'm looking to make heavy consumption of a RESTful API from a ColdFusion application.

I'm not a CF expert but I'm anticipating the repeated cfhttp calls will become a bottleneck as I believe that each results in a connection being established, request sent, response received and connection torn down.

I'm curious - is there a way to maintain a connection pool that requests could be sent through to avoid the repeated establish/tear down?

Does the ColdFusion server provide such a facility that I just don't know about (we're using CF 8) or can I write a java custom tag that could maintain the pool?

Certainly someone else has encountered this.

Saltandpepper answered 9/3, 2012 at 17:3 Comment(0)
L
0

Unfortunately I think the answer is, "no", specifically because of your requirements. That's just not how REST works; and the limitation is the API side, not a ColdFusion issue.

You could do something similar, assuming you had control over the API end of things too, but it wouldn't be REST.

Liturgics answered 9/3, 2012 at 17:51 Comment(1)
Are you completely sure about that? It seems like if you could keep alive an HTTP 1.1 connection to the backend, you could issue multiple REST API requests from within that context.Isallobar
I
0

I think you might actually be able to do this by using the "Keep-Alive" request header with your cfhttp calls. For example:

<cfloop from="1" to="50" index="i">
  <cfhttp url="http://mysite.com/getPage.cfm?i=#i#" method="get">
    <cfif i LT 50>
    <CFHTTPPARAM type="HEADER" name="Connection" value="Keep-Alive">
    <cfelse>
    <CFHTTPPARAM type="HEADER" name="Connection" value="close">
    </cfif>
  </cfhttp>

  <cfdump var="#cfhttp.filecontent#">

</cfloop>

I haven't tested this, but in theory it should keep the connection to the back end open while you make each of those requests (assuming the backend allows for this and the delay between connections hasn't triggered a time-out). You should be sure that your API response includes a "Content-length" header so that the client (your cfhttp code) will know when each request has completed. You will want to issue an explicit "close" as I've shown to prevent unneeded open connections to the backend.

Isallobar answered 9/3, 2012 at 18:56 Comment(4)
That's an interesting thought and might have some applicability if you're making multiple calls to the REST resource on the same page. Not sure what happens with that connection after page execution stops but I can guess.Saltandpepper
That's an interesting thought and might have some applicability if you're making multiple calls to the REST resource on the same page. Not sure what happens with that connection after page execution stops but I can guess. I was thinking more of something that could maintain a connection pool at the server scope and that could persist across requests. Similar to built-in database connection pools. So create a java object at server scope and run all REST reqs through it instead of cfhttp calls and the java object is maintaining a pool. Not even sure that would work, but that's the theory...Saltandpepper
That might be possible, but it would certainly have to be something done at the java level.Isallobar
The keep-alive with CFHTTP does not work. ColdFusion sends a close connection after the keep-alive in the header.Isochronous

© 2022 - 2024 — McMap. All rights reserved.