Permalink
Browse files

fixed the condition where a pending ports.get() would get lost on a h…

…ub crash and so connections would drop and not reconnect
  • Loading branch information...
1 parent bcccaf2 commit b5313f4569380b0c585279d9275e5495c85c07af @substack committed May 24, 2012
Showing with 14 additions and 6 deletions.
  1. +13 −5 index.js
  2. +1 −1 package.json
View
@@ -32,7 +32,6 @@ function Airport (ports, cons) {
Airport.prototype.connect = function (role, fn) {
var ports = this.ports;
- var up = ports.up;
var cons = this.cons;
function ondown () {
@@ -41,7 +40,7 @@ Airport.prototype.connect = function (role, fn) {
ports.get(role, onget);
function onget (ps) {
- up.removeListener('down', ondown);
+ ports.removeListener('down', ondown);
var s = pick(ps);
if (res) res.destroy();
@@ -57,7 +56,7 @@ Airport.prototype.connect = function (role, fn) {
queue.forEach(function (cb) { res(cb) });
queue = [];
}
- up.on('down', ondown);
+ ports.on('down', ondown);
function connector (service, cb) {
var inst = upnode(cons);
@@ -95,7 +94,16 @@ Airport.prototype.connect = function (role, fn) {
if (pending) return;
target.emit('reconnect');
- ports.get(role, function (ps) {
+ function onup () {
+ if (!active || !pending) return;
+ ports.get(role, withResults);
+ }
+ ports.once('up', onup);
+ ports.get(role, withResults);
+
+ function withResults (ps) {
+ ports.removeListener('up', onup);
+
if (!active) return;
pending = false;
var s = pick(ps);
@@ -105,7 +113,7 @@ Airport.prototype.connect = function (role, fn) {
c.close();
cb(s);
}
- });
+ }
pending = true;
}
c.on('reconnect', onreconnect);
View
@@ -1,7 +1,7 @@
{
"name" : "airport",
"description" : "role-based port management for upnode",
- "version" : "0.3.8",
+ "version" : "0.3.9",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/airport.git"

0 comments on commit b5313f4

Please sign in to comment.