]> git.sesse.net Git - remoteglot-book/blob - www/serve-analysis.js
Make some constants SHOUT_A_BIT.
[remoteglot-book] / www / serve-analysis.js
1 // node.js version of analysis.pl; hopefully scales a bit better
2 // for this specific kind of task.
3
4 // Modules.
5 var http = require('http');
6 var fs = require('fs');
7 var url = require('url');
8 var querystring = require('querystring');
9 var path = require('path');
10 var zlib = require('zlib');
11 var delta = require('./js/json_delta.js');
12
13 // Constants.
14 var JSON_FILENAME = '/srv/analysis.sesse.net/www/analysis.json';
15 var HISTORY_TO_KEEP = 5;
16
17 // The current contents of the file to hand out, and its last modified time.
18 var json = undefined;
19
20 // The last five timestamps, and diffs from them to the latest version.
21 var historic_json = [];
22 var diff_json = {};
23
24 // The list of clients that are waiting for new data to show up.
25 // Uniquely keyed by request_id so that we can take them out of
26 // the queue if they close the socket.
27 var sleeping_clients = {};
28 var request_id = 0;
29
30 // List of when clients were last seen, keyed by their unique ID.
31 // Used to show a viewer count to the user.
32 var last_seen_clients = {};
33
34 // The timer used to touch the file every 30 seconds if nobody
35 // else does it for us. This makes sure we don't have clients
36 // hanging indefinitely (which might have them return errors).
37 var touch_timer = undefined;
38
39 // If we are behind Varnish, we can't count the number of clients
40 // ourselves, so some external log-tailing daemon needs to tell us.
41 var viewer_count_override = undefined;
42
43 var replace_json = function(new_json_contents, mtime) {
44         // Generate the list of diffs from the last five versions.
45         if (json !== undefined) {
46                 // If two versions have the same mtime, clients could have either.
47                 // Note the fact, so that we never insert it.
48                 if (json.last_modified == mtime) {
49                         json.invalid_base = true;
50                 }
51                 if (!json.invalid_base) {
52                         historic_json.push(json);
53                         if (historic_json.length > HISTORY_TO_KEEP) {
54                                 historic_json.shift();
55                         }
56                 }
57         }
58
59         var new_json = {
60                 parsed: JSON.parse(new_json_contents),
61                 plain: new_json_contents,
62                 last_modified: mtime
63         };
64         create_json_historic_diff(new_json, historic_json.slice(0), {}, function(new_diff_json) {
65                 // gzip the new version (non-delta), and put it into place.
66                 zlib.gzip(new_json_contents, function(err, buffer) {
67                         if (err) throw err;
68
69                         new_json.gzip = buffer;
70                         json = new_json;
71                         diff_json = new_diff_json;
72
73                         // Finally, wake up any sleeping clients.
74                         possibly_wakeup_clients();
75                 });
76         });
77 }
78
79 var create_json_historic_diff = function(new_json, history_left, new_diff_json, cb) {
80         if (history_left.length == 0) {
81                 cb(new_diff_json);
82                 return;
83         }
84
85         var histobj = history_left.shift();
86         var diff = delta.JSON_delta.diff(histobj.parsed, new_json.parsed);
87         var diff_text = JSON.stringify(diff);
88         zlib.gzip(diff_text, function(err, buffer) {
89                 if (err) throw err;
90                 new_diff_json[histobj.last_modified] = {
91                         plain: diff,
92                         text: diff_text,
93                         gzip: buffer,
94                         last_modified: new_json.last_modified,
95                 };
96                 create_json_historic_diff(new_json, history_left, new_diff_json, cb);
97         });
98 }
99
100 var reread_file = function(event, filename) {
101         if (filename != path.basename(JSON_FILENAME)) {
102                 return;
103         }
104         console.log("Rereading " + JSON_FILENAME);
105         fs.open(JSON_FILENAME, 'r+', function(err, fd) {
106                 if (err) throw err;
107                 fs.fstat(fd, function(err, st) {
108                         if (err) throw err;
109                         var buffer = new Buffer(1048576);
110                         fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) {
111                                 if (err) throw err;
112                                 fs.close(fd, function() {
113                                         var new_json_contents = buffer.toString('utf8', 0, bytesRead);
114                                         replace_json(new_json_contents, st.mtime.getTime());
115                                 });
116                         });
117                 });
118         });
119
120         if (touch_timer !== undefined) {
121                 clearTimeout(touch_timer);
122         }
123         touch_timer = setTimeout(function() {
124                 console.log("Touching analysis.json due to no other activity");
125                 var now = Date.now() / 1000;
126                 fs.utimes(JSON_FILENAME, now, now);
127         }, 30000);
128 }
129 var possibly_wakeup_clients = function() {
130         var num_viewers = count_viewers();
131         for (var i in sleeping_clients) {
132                 mark_recently_seen(sleeping_clients[i].unique);
133                 send_json(sleeping_clients[i].response,
134                           sleeping_clients[i].ims,
135                           sleeping_clients[i].accept_gzip,
136                           num_viewers);
137         }
138         sleeping_clients = {};
139 }
140 var send_404 = function(response) {
141         response.writeHead(404, {
142                 'Content-Type': 'text/plain',
143         });
144         response.write('Something went wrong. Sorry.');
145         response.end();
146 }
147 var handle_viewer_override = function(request, u, response) {
148         // Only accept requests from localhost.
149         var peer = request.socket.localAddress;
150         if ((peer != '127.0.0.1' && peer != '::1') || request.headers['x-forwarded-for']) {
151                 console.log("Refusing viewer override from " + peer);
152                 send_404(response);
153         } else {
154                 viewer_count_override = (u.query)['num'];
155                 response.writeHead(200, {
156                         'Content-Type': 'text/plain',
157                 });
158                 response.write('OK.');
159                 response.end();
160         }
161 }
162 var send_json = function(response, ims, accept_gzip, num_viewers) {
163         var this_json = diff_json[ims] || json;
164
165         var headers = {
166                 'Content-Type': 'text/json',
167                 'X-Remoteglot-Last-Modified': this_json.last_modified,
168                 'X-Remoteglot-Num-Viewers': num_viewers,
169                 'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
170                 'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
171                 'Vary': 'Accept-Encoding',
172         };
173
174         if (accept_gzip) {
175                 headers['Content-Encoding'] = 'gzip';
176                 response.writeHead(200, headers);
177                 response.write(this_json.gzip);
178         } else {
179                 response.writeHead(200, headers);
180                 response.write(this_json.text);
181         }
182         response.end();
183 }
184 var mark_recently_seen = function(unique) {
185         if (unique) {
186                 last_seen_clients[unique] = (new Date).getTime();
187         }
188 }
189 var count_viewers = function() {
190         if (viewer_count_override !== undefined) {
191                 return viewer_count_override;
192         }
193
194         var now = (new Date).getTime();
195
196         // Go through and remove old viewers, and count them at the same time.
197         var new_last_seen_clients = {};
198         var num_viewers = 0;
199         for (var unique in last_seen_clients) {
200                 if (now - last_seen_clients[unique] < 5000) {
201                         ++num_viewers;
202                         new_last_seen_clients[unique] = last_seen_clients[unique];
203                 }
204         }
205
206         // Also add sleeping clients that we would otherwise assume timed out.
207         for (var request_id in sleeping_clients) {
208                 var unique = sleeping_clients[request_id].unique;
209                 if (unique && !(unique in new_last_seen_clients)) {
210                         ++num_viewers;
211                 }
212         }
213
214         last_seen_clients = new_last_seen_clients;
215         return num_viewers;
216 }
217
218 // Set up a watcher to catch changes to the file, then do an initial read
219 // to make sure we have a copy.
220 fs.watch(path.dirname(JSON_FILENAME), reread_file);
221 reread_file(null, path.basename(JSON_FILENAME));
222
223 var server = http.createServer();
224 server.on('request', function(request, response) {
225         var u = url.parse(request.url, true);
226         var ims = (u.query)['ims'];
227         var unique = (u.query)['unique'];
228
229         console.log((new Date).getTime()*1e-3 + " " + request.url);
230         if (u.pathname === '/override-num-viewers') {
231                 handle_viewer_override(request, u, response);
232                 return;
233         }
234         if (u.pathname !== '/analysis.pl') {
235                 // This is not the request you are looking for.
236                 send_404(response);
237                 return;
238         }
239
240         mark_recently_seen(unique);
241
242         var accept_encoding = request.headers['accept-encoding'];
243         var accept_gzip;
244         if (accept_encoding !== undefined && accept_encoding.match(/\bgzip\b/)) {
245                 accept_gzip = true;
246         } else {
247                 accept_gzip = false;
248         }
249
250         // If we already have something newer than what the user has,
251         // just send it out and be done with it.
252         if (json !== undefined && (!ims || json.last_modified > ims)) {
253                 send_json(response, ims, accept_gzip, count_viewers());
254                 return;
255         }
256
257         // OK, so we need to hang until we have something newer.
258         // Put the user on the wait list.
259         var client = {};
260         client.response = response;
261         client.request_id = request_id;
262         client.accept_gzip = accept_gzip;
263         client.unique = unique;
264         client.ims = ims;
265         sleeping_clients[request_id++] = client;
266
267         request.socket.client = client;
268 });
269 server.on('connection', function(socket) {
270         socket.on('close', function() {
271                 var client = socket.client;
272                 if (client) {
273                         mark_recently_seen(client.unique);
274                         delete sleeping_clients[client.request_id];
275                 }
276         });
277 });
278 server.listen(5000);