Permalink
Browse files

Add an optional delay before to increment a namespace counter when ne…

…w client connects

This allows to mitigate invalid counting due to buggy clients that connects/disconnects/... in loop
  • Loading branch information...
1 parent 88d5ca0 commit 50767265f71dfafc56bc9dd1962ad29b3e1e1de1 @rs committed Feb 14, 2012
Showing with 19 additions and 4 deletions.
  1. +2 −0 audience-meter.js
  2. +17 −4 lib/worker.js
View
2 audience-meter.js
@@ -19,6 +19,7 @@ options
.option('--notify-min-delay <seconds>', 'Minimum delay between notifications (default 2)', parseFloat, 2)
.option('--notify-max-delay <seconds>', 'Maximum delay to wait before not sending notification ' +
'because of min-delta not reached (default 25)', parseFloat, 25)
+ .option('--increment-delay <seconds>', 'Number of seconds to wait before to increment the counter in order to mitigate de/connection floods', parseFloat, 0)
.option('--namespace-clean-delay <seconds>', 'Minimum delay to wait before to clean an empty namespace (default 60)', parseFloat, 60)
.option('--demo-port <port>', 'Public port on which to bind the demo server (default 8080, 0 to disable)', parseInt, 8080)
.option('--stats-port <port>', 'Local port on which to bind the global stats server (default 1442, 0 to disable)', parseInt, 1442)
@@ -86,6 +87,7 @@ else
require('./lib/worker').Worker
({
uuid: options.enableUuid,
+ increment_delay: options.incrementDelay,
log: logger
});
}
View
21 lib/worker.js
@@ -9,9 +9,10 @@ function Worker(options)
{
if (!(this instanceof Worker)) return new Worker(options);
- options = merge
+ this.options = options = merge
({
uuid: false,
+ increment_delay: 0,
log: function(severity, message) {console.log(message);}
}, options);
@@ -121,12 +122,24 @@ Worker.prototype.join = function(namespace, client, uuid)
var self = this;
// Subscribe
this.groups.get(namespace).addClient(client);
- // Increment namespace counter on master
- process.send({cmd: 'join', namespace: namespace});
+ // Increment namespace counter on master (after a configured delay to mitigate +/- flood)
+ client.incrementTimeout = setTimeout(function()
+ {
+ process.send({cmd: 'join', namespace: namespace});
+ delete client.incrementTimeout;
+ }, this.options.increment_delay * 1000);
// Decrement on exit
client.socket.on('close', function()
{
- process.send({cmd: 'leave', namespace: namespace});
+ // Notify about the decrement only if the increment happended
+ if ('incrementTimeout' in client)
+ {
+ clearTimeout(client.incrementTimeout);
+ }
+ else
+ {
+ process.send({cmd: 'leave', namespace: namespace});
+ }
if (uuid && self.clientRegistry[uuid] == client)
{

0 comments on commit 5076726

Please sign in to comment.