Skip to content

Commit bf958d0

Browse files
committed
Bind underlying connections in pool to same domain as pool
fixes #1242
1 parent 6aeaed6 commit bf958d0

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ you spot any mistakes.
88

99
* Add `POOL_CLOSED` code to "Pool is closed." error
1010
* Add `POOL_CONNLIMIT` code to "No connections available." error #1332
11+
* Bind underlying connections in pool to same domain as pool #1242
1112
* Bind underlying socket to same domain as connection #1243
1213
* Fix edge cases constructing long stack traces #1387
1314
* Fix Query stream to emit close after ending #1349 #1350

lib/PoolConnection.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var inherits = require('util').inherits;
1+
var inherits = require('util').inherits;
22
var Connection = require('./Connection');
3+
var Events = require('events');
34

45
module.exports = PoolConnection;
56
inherits(PoolConnection, Connection);
@@ -8,6 +9,17 @@ function PoolConnection(pool, options) {
89
Connection.call(this, options);
910
this._pool = pool;
1011

12+
// Bind connection to pool domain
13+
if (Events.usingDomains) {
14+
if (this.domain) {
15+
this.domain.remove(this);
16+
}
17+
18+
if (pool.domain) {
19+
pool.domain.add(this);
20+
}
21+
}
22+
1123
// When a fatal error occurs the connection's protocol ends, which will cause
1224
// the connection to end as well, thus we only need to watch for the end event
1325
// and we will be notified of disconnects.
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var after = require('after');
2+
var assert = require('assert');
3+
var common = require('../../common');
4+
var domain = null;
5+
6+
try {
7+
domain = require('domain');
8+
} catch (e) {
9+
common.skipTest('node ' + process.version + ' does not support domains');
10+
}
11+
12+
var d0 = domain.create();
13+
var d1 = domain.create();
14+
15+
var server = common.createFakeServer();
16+
17+
var pool = null;
18+
var done = after(2, function () {
19+
pool.end(function (err) {
20+
assert.ifError(err);
21+
server.destroy();
22+
});
23+
});
24+
25+
server.listen(common.fakeServerPort, function (err) {
26+
assert.ifError(err);
27+
28+
var released = false;
29+
var timer = setInterval(function () {
30+
if (!released) return;
31+
clearInterval(timer);
32+
33+
assert.ok(!domain.active, 'no current domain');
34+
pool.getConnection(function (err, conn) {
35+
assert.ifError(err);
36+
assert.equal(domain.active, d0, 'current domain is d0');
37+
assert.equal(conn.domain, d0, 'connection domain is d0');
38+
conn.release();
39+
done();
40+
});
41+
}, 200);
42+
43+
d0.run(function () {
44+
pool = common.createPool({port: common.fakeServerPort, connectionLimit: 1});
45+
assert.equal(pool.domain, d0, 'pool belongs to d0');
46+
47+
d1.run(function () {
48+
pool.getConnection(function (err, conn) {
49+
assert.ifError(err);
50+
assert.equal(domain.active, d1, 'current domain is d1');
51+
assert.equal(conn.domain, d0, 'connection domain is d0');
52+
conn.release();
53+
released = true;
54+
done();
55+
});
56+
});
57+
});
58+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var after = require('after');
2+
var assert = require('assert');
3+
var common = require('../../common');
4+
var domain = null;
5+
6+
try {
7+
domain = require('domain');
8+
} catch (e) {
9+
common.skipTest('node ' + process.version + ' does not support domains');
10+
}
11+
12+
var d0 = domain.create();
13+
14+
var server = common.createFakeServer();
15+
16+
var pool = null;
17+
var done = after(2, function () {
18+
pool.end(function (err) {
19+
assert.ifError(err);
20+
server.destroy();
21+
});
22+
});
23+
24+
server.listen(common.fakeServerPort, function (err) {
25+
assert.ifError(err);
26+
27+
var released = false;
28+
var timer = setInterval(function () {
29+
if (!released) return;
30+
clearInterval(timer);
31+
32+
assert.ok(!domain.active, 'no current domain');
33+
pool.getConnection(function (err, conn) {
34+
assert.ifError(err);
35+
assert.ok(!domain.active, 'no current domain');
36+
assert.equal(conn.domain, null, 'connection is not bound to domain');
37+
conn.release();
38+
done();
39+
});
40+
}, 200);
41+
42+
pool = common.createPool({port: common.fakeServerPort, connectionLimit: 1});
43+
assert.equal(pool.domain, null, 'pool is not bound to domain');
44+
45+
d0.run(function () {
46+
pool.getConnection(function (err, conn) {
47+
assert.ifError(err);
48+
assert.equal(domain.active, d0, 'current domain is d0');
49+
assert.equal(conn.domain, null, 'connection is not bound to domain');
50+
conn.release();
51+
released = true;
52+
done();
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)