forked from faye/faye-redis-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaye-redis.js
443 lines (371 loc) · 13.8 KB
/
faye-redis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Constructor for multiRedis. It sets up two connections for each provided
// Redis URL and adds them to a ketema ring. One connection is used for
// commands and the other is used for pub/sub subscriptions.
var multiRedis = function(urls) {
var hasher = require('consistent-hashing'),
self = this;
self.ring = new hasher(urls);
self.urls = urls;
self.connections = {};
self.subscriptions = {};
urls.forEach(function(url) {
var options = self.parse(url);
var connection = self.connect(options);
var subscription = self.connect(options);
self.connections[url] = connection;
self.subscriptions[url] = subscription;
});
};
// [ command, argument-to-shard-against ]
multiRedis.COMMANDS = [
['smembers', 0],
['del', 0],
['sadd', 0],
['srem', 0],
['rpush', 0],
['expire', 0],
['get', 0],
['getset', 0],
['zrem', 1],
['zadd', 2],
['zscore', 1]
];
multiRedis.prototype = {
// Grab the connection from the ring for the pub/sub server for the message
// and delegate a publish call to it.
publish: function(topic, message) {
var connection = this.connectionFor(message);
connection.publish.apply(connection, arguments);
},
// Subscribe to the topic on all of the subscription connections and call
// the callback on a new message.
subscribe: function(topic, callback) {
var self = this;
self.urls.forEach(function(url) {
var subscription = self.subscriptions[url];
subscription.subscribe(topic);
subscription.on('message', callback);
});
},
// Returns a connection based on a single key for dispatching multiple
// connections atomically. You should only commit operations against a single
// key during a multi due to the sharding.
multi: function(key) {
return this.connectionFor(key).multi();
},
// Returns a new Redis connection. Expects a server configuration object,
// e.g.:
//
// { port: 6379,
// hostname: 'localhost',
// database: 0,
// password: 'chunkybacon' }
connect: function(server) {
var redis = require('redis'),
connection = redis.createClient(server.port, server.hostname);
connection.select(server.database);
if (server.password)
connection.auth(server.password);
return connection;
},
// Parses a URL and returns a server configuration object, e.g.:
//
// redis://:chunkybacon@localhost:6379/0
parse: function(url) {
var url = require('url').parse(url),
connection = { hostname: url.hostname, port: url.port };
if (url.auth)
connection.password = url.auth.split(":")[1];
if (url.path) {
connection.database = url.path.substring(1);
} else {
connection.database = 0;
}
return connection;
},
// Closes all connections to Redis.
end: function() {
var self = this;
self.urls.forEach(function(url) {
self.connections[url].end();
self.subscriptions[url].unsubscribe();
self.subscriptions[url].end();
});
},
// Returns a connection for a given key.
connectionFor: function(key) {
return this.connections[this.ring.getNode(key)];
}
};
// Loops through the commands and adds each one to multiRedis.
multiRedis.COMMANDS.forEach(function(command) {
var redisCommand = command[0],
argument = command[1];
multiRedis.prototype[redisCommand] = function() {
var connection = this.connectionFor(arguments[argument]);
return connection[redisCommand].apply(connection, arguments);
}
});
// Creates a new Faye Redis engine.
//
// Options:
// disable_subscriptions If set to `true`, then this engine will not subscribe
// to the notifications channel.
//
// gc When `true`, GC is run continuously in this process.
// Seeing as how it's no longer interval-based, you
// probably only want to set this in a dedicated GC
// process.
//
var Engine = function(server, options) {
this._options = options || {};
var self = this;
this._server = server;
this._ns = this._options.namespace || '';
this._redis = new multiRedis(options.servers);
if (!this._options.disable_subscriptions) {
this._redis.subscribe(this._ns + '/notifications', function(topic, message) {
self.emptyQueue(message);
});
}
if (this._options.gc) {
if (process.env.STATSD_URL) {
var url = require("url");
var statsd = require("node-statsd").StatsD;
var statsdUrl = url.parse(process.env.STATSD_URL);
var prefix = "push." + process.env.NODE_ENV + ".";
this.statsd = new statsd(statsdUrl.hostname, statsdUrl.port, prefix);
}
this.gc();
}
};
Engine.create = function(server, options) {
return new this(server, options);
};
Engine.prototype = {
DEFAULT_GC: 60,
LOCK_TIMEOUT: 120,
disconnect: function() {
this._redis.end();
clearInterval(this._gc);
},
createClient: function(callback, context) {
var clientId = this._server.generateId(),
score = new Date().getTime(),
self = this;
this._redis.zadd(this._ns + '/clients', score, clientId, function(error, added) {
if (added === 0) return self.createClient(callback, context);
self._server.debug('Created new client ? with score ?', clientId, score);
self._server.trigger('handshake', clientId);
callback.call(context, clientId);
});
},
clientExists: function(clientId, callback, context) {
var timeout = this._server.timeout;
if (clientId === undefined) {
this._server.debug("[RedisEngine#clientExists] undefined clientId, returning false");
return callback.call(context, false);
}
this._redis.zscore(this._ns + '/clients', clientId, function(error, score) {
if (timeout) {
callback.call(context, score > new Date().getTime() - 1000 * 1.75 * timeout);
} else {
callback.call(context, score !== null);
}
});
},
// Destroy a client.
//
// The first part of cleaning up a client is removing subscriptions, which
// removes the client ID from all the channels that it's a member of. This
// prevents messages from being published to that client.
//
// In a reversal of earlier behavior, callbacks are now _always_ called,
// but with an argument that indicates whether or not the destroy actually
// succeeded.
destroyClient: function(clientId, callback, context) {
var self = this;
var clientChannelsKey = this._ns + "/clients/" + clientId + "/channels";
this._redis.smembers(clientChannelsKey, function(error, channels) {
if (error) {
return self._failGC(callback, context, "Failed to fetch channels ?: ?", clientChannelsKey, error);
}
var numChannels = channels.length, numUnsubscribes = 0;
if (numChannels == 0) {
return self._deleteClient(clientId, callback, context);
}
channels.forEach(function(channel) {
var channelsKey = self._ns + "/channels" + channel;
self._redis.srem(channelsKey, clientId, function(error, res) {
if (error) {
return self._failGC(callback, context, "Failed to remove client ? from ?: ?", clientId, channelsKey, error);
}
numUnsubscribes += 1;
self._server.trigger("unsubscribe", clientId, channel);
if (numUnsubscribes == numChannels) {
self._deleteClient(clientId, callback, context);
}
});
});
});
},
// Removes the client bookkeeping records.
//
// Finishes client cleanup by removing the mailbox, channel set, and finally
// the client ID from the sorted set. Once again, any Redis errors shut down
// the callback chain, and we'll rely on GC to pick it back up again.
_deleteClient: function(clientId, callback, context) {
var self = this,
clientChannelsKey = this._ns + "/clients/" + clientId + "/channels",
clientMessagesKey = this._ns + "/clients/" + clientId + "/messages";
this._redis.del(clientChannelsKey);
this._redis.del(clientMessagesKey);
this._redis.zrem(self._ns + "/clients", clientId, function(error, res) {
if (error) {
return self._failGC(callback, context, "Failed to remove client ID ? from /clients: ?", clientId, error);
}
self._server.debug("Destroyed client ? successfully", clientId);
self._server.trigger("disconnect", clientId);
if (self.statsd) {
self.statsd.increment("gc.success");
}
if (callback) {
callback.call(context, true);
}
});
},
ping: function(clientId) {
var timeout = this._server.timeout;
if (typeof timeout !== 'number') return;
var time = new Date().getTime();
this._server.debug('Ping ?, ?', clientId, time);
this._redis.zadd(this._ns + '/clients', time, clientId);
},
subscribe: function(clientId, channel, callback, context) {
var self = this;
this._redis.sadd(this._ns + '/clients/' + clientId + '/channels', channel, function(error, added) {
if (added === 1) self._server.trigger('subscribe', clientId, channel);
});
this._redis.sadd(this._ns + '/channels' + channel, clientId, function() {
self._server.debug('Subscribed client ? to channel ?', clientId, channel);
if (callback) callback.call(context);
});
},
unsubscribe: function(clientId, channel, callback, context) {
var self = this;
this._redis.srem(this._ns + '/clients/' + clientId + '/channels', channel, function(error, removed) {
if (removed === 1) self._server.trigger('unsubscribe', clientId, channel);
});
this._redis.srem(this._ns + '/channels' + channel, clientId, function() {
self._server.debug('Unsubscribed client ? from channel ?', clientId, channel);
if (callback) callback.call(context);
});
},
publish: function(message, channels) {
this._server.debug('Publishing message ?', message);
var self = this,
notified = [],
jsonMessage = JSON.stringify(message),
keys = channels.map(function(c) { return self._ns + '/channels' + c });
var notify = function(error, clients) {
if (error) {
return self._server.error("Failed to fetch clients, candidate channels ?: ?", keys, error);
}
clients.forEach(function(clientId) {
if (notified.indexOf(clientId) == -1) {
self.clientExists(clientId, function(exists) {
if (exists) {
self._server.debug('Queueing for client ?: ?', clientId, message);
self._redis.rpush(self._ns + '/clients/' + clientId + '/messages', jsonMessage);
self._redis.publish(self._ns + '/notifications', clientId);
self._redis.expire(self._ns + '/clients/' + clientId + '/messages', 3600)
notified.push(clientId);
} else {
self._server.debug("Destroying expired client ? from publish", clientId);
self.destroyClient(clientId);
}
});
}
});
};
keys.forEach(function(key) {
if (key.indexOf("*") == -1)
self._redis.smembers(key, notify);
});
this._server.trigger('publish', message.clientId, message.channel, message.data);
},
emptyQueue: function(clientId) {
if (!this._server.hasConnection(clientId)) return;
var key = this._ns + '/clients/' + clientId + '/messages',
multi = this._redis.multi(key),
self = this;
multi.lrange(key, 0, -1, function(error, jsonMessages) {
var messages = jsonMessages.map(function(json) { return JSON.parse(json) });
self._server.deliver(clientId, messages);
});
multi.del(key);
multi.exec();
},
gc: function() {
var timeout = this._server.timeout;
if (typeof timeout !== 'number') return;
var self = this;
this._redis.urls.forEach(function(url) {
this._server.debug("Starting GC loop for ?", url);
process.nextTick(function() {
this._runGC(url, timeout);
}.bind(this));
// Track the number of clients in each shard with a statsd gauge.
if (this.statsd) {
var host = require("url").parse(url).hostname.replace(/\./g, '_'),
conn = this._redis.connections[url],
self = this,
key = "clients." + host;
setInterval(function() {
conn.zcard(self._ns + "/clients", function(error, n) {
if (!error) {
self.statsd.gauge(key, n);
}
});
}, 10000);
}
}, this);
},
_runGC: function(url, timeout) {
var conn = this._redis.connections[url],
cutoff = new Date().getTime() - 1000 * 2 * timeout,
self = this;
conn.zrangebyscore(this._ns + "/clients", 0, cutoff, "LIMIT", 0, 1, function(error, clients) {
if (error) {
self._server.error("[?] Failed to fetch GC client, retrying in 2 seconds...", url);
return setTimeout(self._runGC.bind(self), 2000, url, timeout);
}
if (clients.length == 0) {
self._server.debug("[?] No GC clients, retrying in 2 seconds...", url);
return setTimeout(self._runGC.bind(self), 2000, url, timeout);
}
var clientId = clients[0];
self.destroyClient(clientId, function(success) {
if (success) {
self._server.debug("[?] GC succeeded for ?", url, clientId);
} else {
self._server.warn("[?] GC failed for ?", url, clientId);
}
process.nextTick(function() {
self._runGC(url, timeout);
}.bind(self));
});
});
},
// A helper function to log a GC error and invoke the callback (if it exists).
_failGC: function(callback, context, msg) {
this._server.error.apply(this._server, Array.prototype.slice.call(arguments, 2, arguments.length));
if (this.statsd) {
this.statsd.increment("gc.failure");
}
if (callback) {
callback.call(context, false);
}
}
};
module.exports = Engine;