]> git.sesse.net Git - remoteglot/blobdiff - www/serve-analysis.js
Solve timeouts by touching analysis.json.
[remoteglot] / www / serve-analysis.js
index fcb91fa83974ee51deef8f538e4666aed228ec2d..362b14b6f0984754b4fcf0f17a6fc7c47d1ad0a6 100644 (file)
@@ -17,9 +17,9 @@ var json_contents = undefined;
 var json_contents_gz = undefined;
 var json_last_modified = undefined;
 
-// The list of clients that are waiting for new data to show up,
-// and their associated timers. Uniquely keyed by request_id
-// so that we can take them out of the queue if they time out.
+// The list of clients that are waiting for new data to show up.
+// Uniquely keyed by request_id so that we can take them out of
+// the queue if they close the socket.
 var sleeping_clients = {};
 var request_id = 0;
 
@@ -27,6 +27,11 @@ var request_id = 0;
 // Used to show a viewer count to the user.
 var last_seen_clients = {};
 
+// The timer used to touch the file every 30 seconds if nobody
+// else does it for us. This makes sure we don't have clients
+// hanging indefinitely (which might have them return errors).
+var touch_timer = undefined;
+
 var reread_file = function(event, filename) {
        if (filename != path.basename(json_filename)) {
                return;
@@ -52,12 +57,23 @@ var reread_file = function(event, filename) {
                        });
                });
        });
+
+       if (touch_timer !== undefined) {
+               clearTimeout(touch_timer);
+       }
+       touch_timer = setTimeout(function() {
+               console.log("Touching analysis.json due to no other activity");
+               var now = Date.now() / 1000;
+               fs.utimes(json_filename, now, now);
+       }, 30000);
 }
 var possibly_wakeup_clients = function() {
+       var num_viewers = count_viewers();
        for (var i in sleeping_clients) {
-               clearTimeout(sleeping_clients[i].timer);
                mark_recently_seen(sleeping_clients[i].unique);
-               send_json(sleeping_clients[i].response, sleeping_clients[i].accept_gzip);
+               send_json(sleeping_clients[i].response,
+                         sleeping_clients[i].accept_gzip,
+                         num_viewers);
        }
        sleeping_clients = {};
 }
@@ -68,11 +84,11 @@ var send_404 = function(response) {
        response.write('Something went wrong. Sorry.');
        response.end();
 }
-var send_json = function(response, accept_gzip) {
+var send_json = function(response, accept_gzip, num_viewers) {
        var headers = {
                'Content-Type': 'text/json',
                'X-Remoteglot-Last-Modified': json_last_modified,
-               'X-Remoteglot-Num-Viewers': count_viewers(),
+               'X-Remoteglot-Num-Viewers': num_viewers,
                'Access-Control-Allow-Origin': 'http://analysis.sesse.net',
                'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
                'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
@@ -88,11 +104,6 @@ var send_json = function(response, accept_gzip) {
        }
        response.end();
 }
-var timeout_client = function(client) {
-       mark_recently_seen(client.unique);
-       send_json(client.response, client.accept_gzip);
-       delete sleeping_clients[client.request_id];
-}
 var mark_recently_seen = function(unique) {
        if (unique) {
                last_seen_clients[unique] = (new Date).getTime();
@@ -155,16 +166,14 @@ server.on('request', function(request, response) {
        // If we already have something newer than what the user has,
        // just send it out and be done with it.
        if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) {
-               send_json(response, accept_gzip);
+               send_json(response, accept_gzip, count_viewers());
                return;
        }
 
        // OK, so we need to hang until we have something newer.
-       // Put the user on the wait list; if we don't get anything
-       // in 30 seconds, though, we'll send something anyway.
+       // Put the user on the wait list.
        var client = {};
        client.response = response;
-       client.timer = setTimeout(function() { timeout_client(client); }, 30000);
        client.request_id = request_id;
        client.accept_gzip = accept_gzip;
        client.unique = unique;