connection-manager
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -19,2 +19,4 @@ /** | ||
var reportingActive = false; | ||
/** | ||
@@ -32,2 +34,24 @@ * Class: ConnectionManager | ||
}); | ||
var self = this; | ||
if (! reportingActive) { | ||
reportingActive = true; | ||
setInterval(function _report() { | ||
self.clients.forEachRecord(function (client) { | ||
var count = client.references.getCount(); | ||
debug('client: ' + client.id + ' reference count: ' + count); | ||
if ((count > 0) && (! client.isConnected())) { | ||
client.connect.apply(buildScope(scope, client), function (err, c) { | ||
if (err) { | ||
debug('failed reconnecting: ', err); | ||
throw new Error(err); | ||
} | ||
debug('client successfully reconnected.'); | ||
client.connection = c; | ||
client = registerListeners(scope, client); | ||
}); | ||
} | ||
}); | ||
}, 60000); | ||
} | ||
} | ||
@@ -230,2 +254,6 @@ | ||
removeListener: o.removeListener, | ||
isConnected: o.isConnected || function () { | ||
debug('default isConnected function is being called, this. might cause dead connections to never reset.'); | ||
return true; | ||
}, | ||
disconnect: o.disconnect, | ||
@@ -236,2 +264,4 @@ connection: undefined | ||
// call provided connect function | ||
var abort = false, | ||
connected = false; | ||
o.connect.call(buildScope(scope, client), function (err, c) { | ||
@@ -242,3 +272,11 @@ if (err) { | ||
return cb(err); | ||
} else if (abort) { | ||
client.connection = c; | ||
client.disconnect.apply(buildScope(scope, client), [function () { | ||
self.clients.removeRecord(client.id); | ||
}]); | ||
debug('skipping connect due to abort timeout.'); | ||
return cb('skipping connect due to abort timeout.'); // abort, callback already called | ||
} | ||
connected = true; | ||
debug('connect call completed without error'); | ||
@@ -250,5 +288,12 @@ client.connection = c; | ||
self.add(scope, client); | ||
debug('calling callback'); | ||
cb(null, client); | ||
}); | ||
setTimeout(function () { | ||
if (! connected) { | ||
abort = true; | ||
cb('unable to connect, timeout [' + o.timeout + '] reached.'); | ||
} | ||
}, o.timeout); | ||
}; | ||
@@ -320,10 +365,13 @@ | ||
var client = this.clients.getRecord(id); | ||
if (client) { | ||
// first thing is we decrese the count by removing the session | ||
client.references.removeRecord(scope.id); | ||
} else { | ||
if (! client) { | ||
debug('removal called on non-existant client: ' + id); | ||
return undefined; | ||
} | ||
debug('removing reference ' + id); | ||
// first thing is we decrese the count by removing the session | ||
client.references.removeRecord(scope.id); | ||
if (this.referenceCount(id) <= 0) { | ||
debug('queuing client for possible disconnect.'); | ||
var self = this; | ||
@@ -337,8 +385,8 @@ | ||
if ((!self.clients.getRecord(id))) { | ||
// console.log('error: skipping duplicate removals, should not arrive here'); | ||
debug('error: skipping duplicate removals, should not arrive here'); | ||
} else if (self.referenceCount(id) <= 0) { | ||
// disconnect client | ||
try { | ||
// console.log('ending client ' + id); | ||
// console.log('references: ', client.references.getIdentifiers()); | ||
debug('ending client ' + id); | ||
debug('references: ', client.references.getIdentifiers()); | ||
client.disconnect.apply(buildScope(scope, client), [function () { | ||
@@ -352,5 +400,7 @@ self.clients.removeRecord(id); | ||
// someone jumped on and grabbed this client | ||
// console.log('client \'' + id + '\' spoken for, aborting removal.'); | ||
debug('client \'' + id + '\' spoken for, aborting removal.'); | ||
} | ||
}, 20000); // delay for 20s | ||
} else { | ||
debug('other references exist, keeping client alive.'); | ||
} | ||
@@ -387,6 +437,9 @@ }; | ||
var self = this; | ||
// make sure we can fetch client object with old creds | ||
var client = ConnectionManager.prototype.get(oldID, oldcreds); | ||
var client = self.get(scope, oldID, oldcreds); | ||
if (! client) { | ||
debug('unable to move, no existing record found for ', oldID); | ||
return false; | ||
@@ -410,3 +463,3 @@ } | ||
removeListener: removeListener, | ||
disconnect: o.disconnect, | ||
disconnect: disconnect, | ||
}; | ||
@@ -413,0 +466,0 @@ |
@@ -47,2 +47,9 @@ /** | ||
o.prototype.removeAll = function () { | ||
var keys = manager.getKeys.call(manager); | ||
for (var i = 0, len = keys.length; i < len; i++) { | ||
manager.remove.call(manager, scope, keys[i]); | ||
} | ||
}; | ||
o.prototype.get = function (id, creds) { | ||
@@ -49,0 +56,0 @@ return manager.get.call(manager, scope, id, creds); |
{ | ||
"name": "connection-manager", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Store, checkout & keepalive active connections from multiple locations in your code-base.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -84,4 +84,4 @@ if (typeof define !== 'function') { | ||
disconnect: false, | ||
listenersConnect: false, | ||
listenersDisconnect: false, | ||
listenersConnected: false, | ||
listenersDisconnected: false, | ||
listenersStar: false | ||
@@ -105,10 +105,10 @@ }; | ||
}, | ||
connect: function (obj) { | ||
connected: function (obj) { | ||
this.scope.connected = true; | ||
checklist.listenersConnect = true; | ||
checklist.listenersConnected = true; | ||
test.assertAnd(this.connection.myConnectionObject, true); | ||
}, | ||
disconnect: function (obj) { | ||
disconnected: function (obj) { | ||
this.scope.disconnected = true; | ||
checklist.listenersDisconnect = true; | ||
checklist.listenersDisconnected = true; | ||
env.callbacks['test-client'].disconnect += 1; | ||
@@ -143,4 +143,4 @@ test.assertAnd(this.connection.myConnectionObject, true); | ||
client.connection.emitter.emit('connect', {hello:'world'}); | ||
client.connection.emitter.emit('disconnect', {hello:'world'}); | ||
client.connection.emitter.emit('connected', {hello:'world'}); | ||
client.connection.emitter.emit('disconnected', {hello:'world'}); | ||
client.connection.emitter.emit('*', {hello:'world'}); | ||
@@ -151,4 +151,4 @@ | ||
disconnect: false, | ||
listenersConnect: true, | ||
listenersDisconnect: true, | ||
listenersConnected: true, | ||
listenersDisconnected: true, | ||
listenersStar: true | ||
@@ -222,3 +222,3 @@ }); | ||
{ | ||
desc: '#get without creds [test-client]', | ||
desc: '# get without creds [test-client]', | ||
run: function (env, test) { | ||
@@ -253,2 +253,26 @@ var client = env.cm1.get('test-client', {}); | ||
{ | ||
desc: '# move [test-client -> test-renamed]', | ||
run: function (env, test) { | ||
test.assert(env.cm1.move( | ||
'test-client', env.credentials['test-client'], | ||
'test-renamed', env.credentials['test-client'] | ||
), true); | ||
} | ||
}, | ||
{ | ||
desc: '# referenceCount 2 [test-renamed]', | ||
run: function (env, test) { | ||
test.assert(env.cm1.referenceCount('test-renamed'), 3); | ||
} | ||
}, | ||
{ | ||
desc: '# get [test-client] should fail', | ||
run: function (env, test) { | ||
test.assert(env.cm1.get('test-client', env.credentials['test-client']), undefined); | ||
} | ||
}, | ||
{ | ||
desc: '# create a client [test-client2]', | ||
@@ -258,5 +282,5 @@ run: function (env, test) { | ||
connect: false, | ||
disconnect: false, | ||
listenersConnect: false, | ||
listenersDisconnect: false | ||
disconnecte: false, | ||
listenersConnected: false, | ||
listenersDisconnected: false | ||
}; | ||
@@ -274,10 +298,10 @@ | ||
listeners: { | ||
connect: function (obj) { | ||
connected: function (obj) { | ||
this.scope.connected = true; | ||
checklist.listenersConnect = true; | ||
checklist.listenersConnected = true; | ||
test.assertAnd(this.connection.myConnectionObject, true); | ||
}, | ||
disconnect: function (obj) { | ||
disconnected: function (obj) { | ||
this.scope.disconnected = true; | ||
checklist.listenersDisconnect = true; | ||
checklist.listenersDisconnected = true; | ||
env.callbacks['test-client2'].disconnect += 1; | ||
@@ -284,0 +308,0 @@ test.assertAnd(this.connection.myConnectionObject, true); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36435
951