X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=www%2Fserve-analysis.js;h=a5713e2d3059ac96df29d4baa2bfd2e4a57d598c;hp=10a3e55545d18b9a5af7cc6143c5899a93ea0f5b;hb=53e5d8fa00e9c8963127ff7ac7c47d078780d24b;hpb=f2f5080432301a5fa444c6b552a648a49889a404 diff --git a/www/serve-analysis.js b/www/serve-analysis.js index 10a3e55..a5713e2 100644 --- a/www/serve-analysis.js +++ b/www/serve-analysis.js @@ -13,6 +13,11 @@ var delta = require('./js/json_delta.js'); // Constants. var JSON_FILENAME = '/srv/analysis.sesse.net/www/analysis.json'; var HISTORY_TO_KEEP = 5; +var MINIMUM_VERSION = null; + +// 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; @@ -69,6 +74,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(); @@ -88,8 +94,8 @@ var create_json_historic_diff = function(new_json, history_left, new_diff_json, zlib.gzip(diff_text, function(err, buffer) { if (err) throw err; new_diff_json[histobj.last_modified] = { - plain: diff, - text: diff_text, + parsed: diff, + plain: diff_text, gzip: buffer, last_modified: new_json.last_modified, }; @@ -101,6 +107,17 @@ var reread_file = function(event, filename) { if (filename != path.basename(JSON_FILENAME)) { return; } + if (json_lock >= 2) { + return; + } + 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; @@ -164,20 +181,25 @@ 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 (MINIMUM_VERSION) { + headers['X-RGMV'] = MINIMUM_VERSION; + } + 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.plain.length; response.writeHead(200, headers); - response.write(this_json.text); + response.write(this_json.plain); } response.end(); } @@ -226,7 +248,7 @@ server.on('request', function(request, response) { var ims = (u.query)['ims']; var unique = (u.query)['unique']; - console.log((new Date).getTime()*1e-3 + " " + request.url); + console.log(((new Date).getTime()*1e-3).toFixed(3) + " " + request.url); if (u.pathname === '/override-num-viewers') { handle_viewer_override(request, u, response); return;