2

I've been having a strange problem with externally linked Javascript libraries hanging my load balancer server running HAProxy. I recently integrated the Quantcast monitoring script, and it worked for about 12 hours, and the next morning I experienced a huge amount of network connections hanging as shown in the graph below. All of the webservers behind my load balancer were completely overloaded with connections as well. I removed Quantcast's script and the problem disappeared.

I've had this problem with some other analytics packages, but have never had this issue with Google Analytics or Adsense (both external libraries). Has anyone ever experienced this or have suggestions on how to avoid it happening?

Netstat Graph

Aldie
  • 146
  • 4

4 Answers4

1

Are you sure it's caused by javascript?

Where did you paste the quantcast tag? Try it in the end of the page, just above , so it doesn't delay loading of other resources (img, css, etc).

Browsers do "hang" if they have to wait for javascript, but that's only because it has to pause the rendering thread to wait for document.write's in the script. It can't keep the tcp connection to port 80 open, except when you have a resource loading issue.

The graph looks more like a out of memory issue, if you run mpm-prefork, make sure MaxClients isn't set too high or you'll run into swap, leading to lots of established connections but nothing being served. Also, check http keepalive settings.

Miles
  • 121
  • 1
  • Yeah - I put this at the end of the page. I've had similar issues with facebook, chartbeat, and other external javascript includes. I haven't had any issues with Yahoo and Google. – Aldie Aug 31 '09 at 10:06
0

We generally cache external javascripts locally. that way you dont have to rely on a third party serving the JS correctly. of course, this might not be an option depending on what the js is doing or if its built dynamically. But you can, for example, host google analytic's js locally.

You can gain some additional advantages to doing this, such as

  1. Gzip - some external js files wont be gzipped
  2. Cache headers - you can set your cache headers to never expire (handling updates with a file name change)
  3. More control - of course the big guys generally arent going to introduce a breaking change, but having externally linked javascript can be risky.
ChickenMilkBomb
  • 419
  • 6
  • 14
0

Using the window.onload event, you can append external Javascript (with simple document.write or DOM construction) only when the page is fully loaded (and the connection closed).

Julien
  • 510
  • 1
  • 5
  • 11
0

Try deferring the running of the script with the defer attribute:

<script src="script.js" type="text/javascript" defer="defer"></script>

It tells the browser not to even think about the script until after the page has loaded. If there are no document.write's in the script it may be okay deferred.