]> git.sesse.net Git - remoteglot/blobdiff - www/serve-analysis.js
More header squeezing.
[remoteglot] / www / serve-analysis.js
index cc1d2dd4303bdf6e0eb9428ec5072ea81c962c42..948ea967c643b5fec2eddd67d02be9606b14f0fe 100644 (file)
@@ -11,13 +11,17 @@ var zlib = require('zlib');
 var delta = require('./js/json_delta.js');
 
 // Constants.
-var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
+var JSON_FILENAME = '/srv/analysis.sesse.net/www/analysis.json';
+var HISTORY_TO_KEEP = 5;
+
+// If set to 1, we are already processing a JSON update and should not
+// start a new one. If set to 2, we are _also_ having one in the queue.
+var json_lock = 0;
 
 // The current contents of the file to hand out, and its last modified time.
 var json = undefined;
 
 // The last five timestamps, and diffs from them to the latest version.
-var history_to_keep = 5;
 var historic_json = [];
 var diff_json = {};
 
@@ -50,7 +54,7 @@ var replace_json = function(new_json_contents, mtime) {
                }
                if (!json.invalid_base) {
                        historic_json.push(json);
-                       if (historic_json.length > history_to_keep) {
+                       if (historic_json.length > HISTORY_TO_KEEP) {
                                historic_json.shift();
                        }
                }
@@ -69,6 +73,7 @@ var replace_json = function(new_json_contents, mtime) {
                        new_json.gzip = buffer;
                        json = new_json;
                        diff_json = new_diff_json;
+                       json_lock = 0;
 
                        // Finally, wake up any sleeping clients.
                        possibly_wakeup_clients();
@@ -98,11 +103,22 @@ var create_json_historic_diff = function(new_json, history_left, new_diff_json,
 }
 
 var reread_file = function(event, filename) {
-       if (filename != path.basename(json_filename)) {
+       if (filename != path.basename(JSON_FILENAME)) {
+               return;
+       }
+       if (json_lock >= 2) {
                return;
        }
-       console.log("Rereading " + json_filename);
-       fs.open(json_filename, 'r+', function(err, fd) {
+       if (json_lock == 1) {
+               // Already processing; wait a bit.
+               json_lock = 2;
+               setTimeout(function() { json_lock = 1; reread_file(event, filename); }, 100);
+               return;
+       }
+       json_lock = 1;
+
+       console.log("Rereading " + JSON_FILENAME);
+       fs.open(JSON_FILENAME, 'r+', function(err, fd) {
                if (err) throw err;
                fs.fstat(fd, function(err, st) {
                        if (err) throw err;
@@ -123,7 +139,7 @@ var reread_file = function(event, filename) {
        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);
+               fs.utimes(JSON_FILENAME, now, now);
        }, 30000);
 }
 var possibly_wakeup_clients = function() {
@@ -164,18 +180,19 @@ var send_json = function(response, ims, accept_gzip, num_viewers) {
 
        var headers = {
                'Content-Type': 'text/json',
-               'X-Remoteglot-Last-Modified': this_json.last_modified,
-               'X-Remoteglot-Num-Viewers': num_viewers,
-               'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
-               'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
+               'X-RGLM': this_json.last_modified,
+               'X-RGNV': num_viewers,
+               'Access-Control-Expose-Headers': 'X-RGLM, X-RGNV',
                'Vary': 'Accept-Encoding',
        };
 
        if (accept_gzip) {
+               headers['Content-Length'] = this_json.gzip.length;
                headers['Content-Encoding'] = 'gzip';
                response.writeHead(200, headers);
                response.write(this_json.gzip);
        } else {
+               headers['Content-Length'] = this_json.text.length;
                response.writeHead(200, headers);
                response.write(this_json.text);
        }
@@ -217,8 +234,8 @@ var count_viewers = function() {
 
 // Set up a watcher to catch changes to the file, then do an initial read
 // to make sure we have a copy.
-fs.watch(path.dirname(json_filename), reread_file);
-reread_file(null, path.basename(json_filename));
+fs.watch(path.dirname(JSON_FILENAME), reread_file);
+reread_file(null, path.basename(JSON_FILENAME));
 
 var server = http.createServer();
 server.on('request', function(request, response) {