Skip to content

Commit

Permalink
Bind underlying connections in pool to same domain as pool
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 1, 2016
1 parent 6aeaed6 commit bf958d0
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ you spot any mistakes.

* Add `POOL_CLOSED` code to "Pool is closed." error
* Add `POOL_CONNLIMIT` code to "No connections available." error #1332
* Bind underlying connections in pool to same domain as pool #1242
* Bind underlying socket to same domain as connection #1243
* Fix edge cases constructing long stack traces #1387
* Fix Query stream to emit close after ending #1349 #1350
Expand Down
14 changes: 13 additions & 1 deletion lib/PoolConnection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var inherits = require('util').inherits;
var inherits = require('util').inherits;
var Connection = require('./Connection');
var Events = require('events');

module.exports = PoolConnection;
inherits(PoolConnection, Connection);
Expand All @@ -8,6 +9,17 @@ function PoolConnection(pool, options) {
Connection.call(this, options);
this._pool = pool;

// Bind connection to pool domain
if (Events.usingDomains) {
if (this.domain) {
this.domain.remove(this);
}

if (pool.domain) {
pool.domain.add(this);
}
}

// When a fatal error occurs the connection's protocol ends, which will cause
// the connection to end as well, thus we only need to watch for the end event
// and we will be notified of disconnects.
Expand Down
58 changes: 58 additions & 0 deletions test/unit/pool/test-connection-domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var after = require('after');
var assert = require('assert');
var common = require('../../common');
var domain = null;

try {
domain = require('domain');
} catch (e) {
common.skipTest('node ' + process.version + ' does not support domains');
}

var d0 = domain.create();
var d1 = domain.create();

var server = common.createFakeServer();

var pool = null;
var done = after(2, function () {
pool.end(function (err) {
assert.ifError(err);
server.destroy();
});
});

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

var released = false;
var timer = setInterval(function () {
if (!released) return;
clearInterval(timer);

assert.ok(!domain.active, 'no current domain');
pool.getConnection(function (err, conn) {
assert.ifError(err);
assert.equal(domain.active, d0, 'current domain is d0');
assert.equal(conn.domain, d0, 'connection domain is d0');
conn.release();
done();
});
}, 200);

d0.run(function () {
pool = common.createPool({port: common.fakeServerPort, connectionLimit: 1});
assert.equal(pool.domain, d0, 'pool belongs to d0');

d1.run(function () {
pool.getConnection(function (err, conn) {
assert.ifError(err);
assert.equal(domain.active, d1, 'current domain is d1');
assert.equal(conn.domain, d0, 'connection domain is d0');
conn.release();
released = true;
done();
});
});
});
});
55 changes: 55 additions & 0 deletions test/unit/pool/test-connection-no-domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var after = require('after');
var assert = require('assert');
var common = require('../../common');
var domain = null;

try {
domain = require('domain');
} catch (e) {
common.skipTest('node ' + process.version + ' does not support domains');
}

var d0 = domain.create();

var server = common.createFakeServer();

var pool = null;
var done = after(2, function () {
pool.end(function (err) {
assert.ifError(err);
server.destroy();
});
});

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

var released = false;
var timer = setInterval(function () {
if (!released) return;
clearInterval(timer);

assert.ok(!domain.active, 'no current domain');
pool.getConnection(function (err, conn) {
assert.ifError(err);
assert.ok(!domain.active, 'no current domain');
assert.equal(conn.domain, null, 'connection is not bound to domain');
conn.release();
done();
});
}, 200);

pool = common.createPool({port: common.fakeServerPort, connectionLimit: 1});
assert.equal(pool.domain, null, 'pool is not bound to domain');

d0.run(function () {
pool.getConnection(function (err, conn) {
assert.ifError(err);
assert.equal(domain.active, d0, 'current domain is d0');
assert.equal(conn.domain, null, 'connection is not bound to domain');
conn.release();
released = true;
done();
});
});
});

0 comments on commit bf958d0

Please sign in to comment.