Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Every HTTP request uses a separate TCP connection

Observations:

  • My web app is running in JBoss.

  • Every single user-click generates >5 HTTP requests because of images etc.

  • Running netstat on the server reveals that a new TCP connection is being opened for every single HTTP request (basically I am looking at the total number of TCP connections from the client IP on port 80).

Facts:

  • JBoss HTTP protocol is set at 1.1.

  • I have checked with FF, IE9 and Chrome - and all browsers do the same.

  • I have two test environments - one running on Windows7 and the other one running on CentOS. I see the same behavior in both.

What I am trying to accomplish

  • Persistent TCP connection, because hopefully that would a) enhance user experience and b) reduce load on the server

At this point, I am not sure what code, configuration details or log I should attach to the question, but if you let me know, I will provide it. Any kind of help is appreciated.

p.s. This thread seemed promising from the title TCP connection is not reused for HTTP requests with HttpURLConnection, but it deals mainly with the client side.

like image 265
Prajesh Bhattacharya Avatar asked Jun 06 '12 15:06

Prajesh Bhattacharya


1 Answers

I think I have found a solution to this. Thanks for the pointers and the suggestions. They really helped.

Part 1: I used the HttpFox plugin in Firefox to look at the response headers. As Philippe suspected the Connection header had a value of "close".

Part 2: Adding a line of code in my own filter to change the response header did not help. So I downloaded and added jbossWebService.jar to the WEB-INF/lib directory in order to use the org.jboss.web.tomcat.filters.ReplyHeaderFilter class. (Prior to JBoss 7, apparently this package used to be included in JBoss by default.) Added the following in my web.xml:

<filter>

<filter-name>CommonHeadersFilter</filter-name>

<filter-class>

org.jboss.web.tomcat.filters.ReplyHeaderFilter</filter-class>

<init-param>

     <param-name>Connection</param-name>

     <param-value>keep-alive</param-value>

</init-param>

</filter>

This did the trick (well, almost). Now, the first "click" from the browser generates about 4 TCP connections - not sure about the reason for that number, because every single click generates >=7 http requests. But all subsequent clicks, if performed within the ttl period (15 s), do not generate additional TCP connections. I guess a more thorough investigation, as suggested by Philippe, would reveal something. But at this point I have to move on. So, for the time being I will mark this question as answered. If needed in the future, I will re-open it.

like image 112
Prajesh Bhattacharya Avatar answered Nov 15 '22 08:11

Prajesh Bhattacharya