问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/9638070/maintain-outbound-tcp-connection-pool-in-coldfusion