X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=www%2Fserve-analysis.js;h=fcb91fa83974ee51deef8f538e4666aed228ec2d;hp=56845eef01f7d0d37391392a1810605441b51dca;hb=87acba69c22f5f745fcf83dc5d9aed1331173079;hpb=a66c165f94e06a675b392d133f60c3dbbab22c60 diff --git a/www/serve-analysis.js b/www/serve-analysis.js index 56845ee..fcb91fa 100644 --- a/www/serve-analysis.js +++ b/www/serve-analysis.js @@ -6,13 +6,16 @@ var http = require('http'); var fs = require('fs'); var url = require('url'); var querystring = require('querystring'); +var path = require('path'); +var zlib = require('zlib'); // Constants. var json_filename = '/srv/analysis.sesse.net/www/analysis.json'; // The current contents of the file to hand out, and its last modified time. -var json_contents = null; -var json_last_modified = null; +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 @@ -24,7 +27,11 @@ var request_id = 0; // Used to show a viewer count to the user. var last_seen_clients = {}; -var reread_file = function() { +var reread_file = function(event, filename) { + if (filename != path.basename(json_filename)) { + return; + } + console.log("Rereading " + json_filename); fs.open(json_filename, 'r+', function(err, fd) { if (err) throw err; fs.fstat(fd, function(err, st) { @@ -33,9 +40,14 @@ var reread_file = function() { fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) { if (err) throw err; fs.close(fd, function() { - json_contents = buffer.toString('utf8', 0, bytesRead); - json_last_modified = st.mtime.getTime(); - possibly_wakeup_clients(); + var new_json_contents = buffer.toString('utf8', 0, bytesRead); + zlib.gzip(new_json_contents, function(err, buffer) { + if (err) throw err; + json_contents = new_json_contents; + json_contents_gz = buffer; + json_last_modified = st.mtime.getTime(); + possibly_wakeup_clients(); + }); }); }); }); @@ -44,7 +56,8 @@ var reread_file = function() { var possibly_wakeup_clients = function() { for (var i in sleeping_clients) { clearTimeout(sleeping_clients[i].timer); - send_json(sleeping_clients[i].response); + mark_recently_seen(sleeping_clients[i].unique); + send_json(sleeping_clients[i].response, sleeping_clients[i].accept_gzip); } sleeping_clients = {}; } @@ -55,22 +68,36 @@ var send_404 = function(response) { response.write('Something went wrong. Sorry.'); response.end(); } -var send_json = function(response) { - response.writeHead(200, { +var send_json = function(response, accept_gzip) { + var headers = { 'Content-Type': 'text/json', 'X-Remoteglot-Last-Modified': json_last_modified, 'X-Remoteglot-Num-Viewers': count_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', - }); - response.write(json_contents); + }; + + if (accept_gzip) { + headers['Content-Encoding'] = 'gzip'; + response.writeHead(200, headers); + response.write(json_contents_gz); + } else { + response.writeHead(200, headers); + response.write(json_contents); + } response.end(); } var timeout_client = function(client) { - send_json(client.response); + 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(); + } +} var count_viewers = function() { var now = (new Date).getTime(); @@ -78,21 +105,31 @@ var count_viewers = function() { 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; } // Set up a watcher to catch changes to the file, then do an initial read // to make sure we have a copy. -fs.watch(json_filename, reread_file); -reread_file(); +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']; @@ -105,14 +142,20 @@ http.createServer(function(request, 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; + if (accept_encoding !== undefined && accept_encoding.match(/\bgzip\b/)) { + accept_gzip = true; + } else { + accept_gzip = false; } // If we already have something newer than what the user has, // just send it out and be done with it. - if (!ims || json_last_modified > ims) { - send_json(response); + if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) { + send_json(response, accept_gzip); return; } @@ -123,5 +166,19 @@ http.createServer(function(request, response) { 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);