views:

845

answers:

1

Background
I use a Tornado-like server with support for long-polls. Each web pages a user clicks around to sets up a long poll to the server like this:

$.ajax({
    type: 'GET',
    url: "/mylongpollurl/",
    dataType: 'application/json',
    success: function(json) {
        // I do stuff here
    },
    error: function(xhr, errText, ex) {
        // If timeout I send a new long-poll request
    }
});

Problem
I will now rely on data that I get from Fiddler monitoring all requests made from my browser (FF at the moment).

  1. Page 1 is loaded and the long poll request is made, now idling at server side.
  2. I click a link to page 2 and that page is loaded and setting up a long poll request, BUT the long poll request from page 1 is still idling at server side (according to Fiddler).

This means that I will stack all long poll calls when clicking around the page, thus end up with lots of active connections on the server (or are they maybe sharing connection?)

My thoughts
- As it's a Tornado-like server (using epoll) it can handle quite a lot of connections. But this fact is not to exploit in my opinion. What I mean is that I prefer not to have a timeout on the server for this case (were the client disappears).
- I know those stand alone pages better uses a common head and only swap content via ajax calls but this design we use today was not my call...
- The best way to solve this would probably be to have the connection reused (hard to pull off I think) or closed as soon as the browser leaves the page (you click to another page).

Thanks
-- MyGGaN

+1  A: 

For long-polling connections, you need to ensure that you have the "Streaming" option set inside Fiddler. Otherwise, Fiddler will keep the connection open waiting indefinitely for the response to finish.

Normally, when you navigate from page to page, the client should tear down the open long-poll connection, effectively closing the connection. I say should because this doesn't always work properly, for instance, when you close a popup window in IE.

EricLaw -MSFT-
Thanks for your input!I've tried to enable streaming in Fiddler and also tried a program called Http debug PRO but they still show that the connection is open. I'll see if I can have a connection counter on the server side.
MyGGaN
You might try having your OnUnload event detach the eventhandlers from the XMLHTTPRequest object. That might improve garbage collection of the object, which in turn may close the underlying connection.
EricLaw -MSFT-
Thanks, I'll try it out and let you know if this solved the problem.
MyGGaN
In jQuery you can have a beforeSend(XMLHttpRequest) function for you're ajax calls. I saved those XHR objects in an array and on window unload I called the .abort() for each object in the array. This actually helped a little bit. But did not close the connection, so I have still a lot of connections open. Further ideas are most welcome! Thanks for this tips EricLaw!
MyGGaN