Fix socket count metric leak

This commit is contained in:
Calvin Montgomery 2018-07-09 20:24:53 -07:00
parent 7acae30875
commit 3db751b65f
3 changed files with 3 additions and 60 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.56.2", "version": "3.56.3",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -218,18 +218,13 @@ class IOServer {
Promise.all(promises).then(() => next()); Promise.all(promises).then(() => next());
} }
metricsEmittingMiddleware(socket, next) {
emitMetrics(socket);
next();
}
handleConnection(socket) { handleConnection(socket) {
// TODO: move out of handleConnection if possible
// see: https://github.com/calzoneman/sync/issues/724
if (!this.checkIPLimit(socket)) { if (!this.checkIPLimit(socket)) {
return; return;
} }
emitMetrics(socket);
LOGGER.info('Accepted socket from %s', socket.context.ipAddress); LOGGER.info('Accepted socket from %s', socket.context.ipAddress);
counters.add('socket.io:accept', 1); counters.add('socket.io:accept', 1);
socket.once('disconnect', () => counters.add('socket.io:disconnect', 1)); socket.once('disconnect', () => counters.add('socket.io:disconnect', 1));
@ -257,7 +252,6 @@ class IOServer {
io.use(this.cookieParsingMiddleware.bind(this)); io.use(this.cookieParsingMiddleware.bind(this));
io.use(this.ipSessionCookieMiddleware.bind(this)); io.use(this.ipSessionCookieMiddleware.bind(this));
io.use(this.authUserMiddleware.bind(this)); io.use(this.authUserMiddleware.bind(this));
io.use(this.metricsEmittingMiddleware.bind(this));
io.on('connection', this.handleConnection.bind(this)); io.on('connection', this.handleConnection.bind(this));
} }

View File

@ -69,57 +69,6 @@ describe('IOServer', () => {
}); });
}); });
/*
describe('#ipConnectionLimitMiddleware', () => {
beforeEach(() => {
socket.once = (event, callback) => {
socket[`on_${event}`] = callback;
};
});
it('allows IPs before the limit', done => {
server.ipConnectionLimitMiddleware(socket, error => {
if (error) {
throw error;
}
done();
});
});
it('rejects IPs at the limit', done => {
server.ipCount.set(socket.context.ipAddress,
require('../../lib/config').get('io.ip-connection-limit'));
server.ipConnectionLimitMiddleware(socket, error => {
assert(error, 'Expected an error to be returned');
assert.strictEqual(error.message,
'Too many connections from your IP address');
done();
});
});
it('manages the ipCount map correctly', done => {
const ip = socket.context.ipAddress;
assert(!server.ipCount.has(ip), 'Test precondition failed: ipCount.has(ip)');
server.ipConnectionLimitMiddleware(socket, error => {
if (error) {
throw error;
}
assert.strictEqual(server.ipCount.get(ip), 1);
socket.on_disconnect();
assert.strictEqual(server.ipCount.get(ip), 0);
done();
});
});
});
*/
describe('#cookieParsingMiddleware', () => { describe('#cookieParsingMiddleware', () => {
it('parses cookies', done => { it('parses cookies', done => {
socket.handshake.headers.cookie = 'flavor=chocolate%20chip'; socket.handshake.headers.cookie = 'flavor=chocolate%20chip';