]> git.sesse.net Git - remoteglot/blobdiff - www/serve-analysis.js
Send the right Vary headers.
[remoteglot] / www / serve-analysis.js
index d1f0bae22eb144deed50d6143ae30f6e0349bf48..868363d0926bc9d49fa8a4539fa0b4798d6765e6 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,15 @@ 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;
+
+// If we are behind Varnish, we can't count the number of clients
+// ourselves, so some external log-tailing daemon needs to tell us.
+var viewer_count_override = undefined;
+
 var reread_file = function(event, filename) {
        if (filename != path.basename(json_filename)) {
                return;
@@ -52,11 +61,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);
-               send_json(sleeping_clients[i].response, sleeping_clients[i].accept_gzip);
+               mark_recently_seen(sleeping_clients[i].unique);
+               send_json(sleeping_clients[i].response,
+                         sleeping_clients[i].accept_gzip,
+                         num_viewers);
        }
        sleeping_clients = {};
 }
@@ -67,14 +88,30 @@ var send_404 = function(response) {
        response.write('Something went wrong. Sorry.');
        response.end();
 }
-var send_json = function(response, accept_gzip) {
+var handle_viewer_override = function(request, u, response) {
+       // Only accept requests from localhost.
+       var peer = request.socket.localAddress;
+       if ((peer != '127.0.0.1' && peer != '::1') || request.headers['x-forwarded-for']) {
+               console.log("Refusing viewer override from " + peer);
+               send_404(response);
+       } else {
+               viewer_count_override = (u.query)['num'];
+               response.writeHead(200, {
+                       'Content-Type': 'text/plain',
+               });
+               response.write('OK.');
+               response.end();
+       }
+}
+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',
+               'Vary': 'Accept-Encoding',
        };
 
        if (accept_gzip) {
@@ -87,22 +124,36 @@ var send_json = function(response, accept_gzip) {
        }
        response.end();
 }
-var timeout_client = function(client) {
-       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();
+       }
 }
 var count_viewers = function() {
+       if (viewer_count_override !== undefined) {
+               return viewer_count_override;
+       }
+
        var now = (new Date).getTime();
 
        // Go through and remove old viewers, and count them at the same time.
        var new_last_seen_clients = {};
        var num_viewers = 0;
        for (var unique in last_seen_clients) {
-               if (now - last_seen_clients[unique] < 60000) {
+               if (now - last_seen_clients[unique] < 5000) {
                        ++num_viewers;
                        new_last_seen_clients[unique] = last_seen_clients[unique];
                }
        }
+
+       // Also add sleeping clients that we would otherwise assume timed out.
+       for (var request_id in sleeping_clients) {
+               var unique = sleeping_clients[request_id].unique;
+               if (unique && !(unique in new_last_seen_clients)) {
+                       ++num_viewers;
+               }
+       }
+
        last_seen_clients = new_last_seen_clients;
        return num_viewers;
 }
@@ -112,22 +163,24 @@ var count_viewers = function() {
 fs.watch(path.dirname(json_filename), reread_file);
 reread_file(null, path.basename(json_filename));
 
-http.createServer(function(request, response) {
+var server = http.createServer();
+server.on('request', function(request, response) {
        var u = url.parse(request.url, true);
        var ims = (u.query)['ims'];
        var unique = (u.query)['unique'];
 
        console.log((new Date).getTime()*1e-3 + " " + request.url);
-
+       if (u.pathname === '/override-num-viewers') {
+               handle_viewer_override(request, u, response);
+               return;
+       }
        if (u.pathname !== '/analysis.pl') {
                // This is not the request you are looking for.
                send_404(response);
                return;
        }
 
-       if (unique) {
-               last_seen_clients[unique] = (new Date).getTime();
-       }
+       mark_recently_seen(unique);
 
        var accept_encoding = request.headers['accept-encoding'];
        var accept_gzip;
@@ -140,17 +193,28 @@ http.createServer(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;
        sleeping_clients[request_id++] = client;
-}).listen(5000);
+
+       request.socket.client = client;
+});
+server.on('connection', function(socket) {
+       socket.on('close', function() {
+               var client = socket.client;
+               if (client) {
+                       mark_recently_seen(client.unique);
+                       delete sleeping_clients[client.request_id];
+               }
+       });
+});
+server.listen(5000);