Skip to content

Commit 6aeaed6

Browse files
committed
Bind underlying socket to same domain as connection
fixes mysqljs#1243
1 parent 5013c07 commit 6aeaed6

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
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 socket to same domain as connection #1243
1112
* Fix edge cases constructing long stack traces #1387
1213
* Fix Query stream to emit close after ending #1349 #1350
1314
* Fix type cast for BIGINT columns when number is negative #1376

lib/Connection.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
var Crypto = require('crypto');
2+
var Events = require('events');
23
var Net = require('net');
34
var tls = require('tls');
45
var ConnectionConfig = require('./ConnectionConfig');
56
var Protocol = require('./protocol/Protocol');
67
var SqlString = require('./protocol/SqlString');
78
var Query = require('./protocol/sequences/Query');
8-
var EventEmitter = require('events').EventEmitter;
99
var Util = require('util');
1010

1111
module.exports = Connection;
12-
Util.inherits(Connection, EventEmitter);
12+
Util.inherits(Connection, Events.EventEmitter);
1313
function Connection(options) {
14-
EventEmitter.call(this);
14+
Events.EventEmitter.call(this);
1515

1616
this.config = options.config;
1717

@@ -90,6 +90,17 @@ Connection.prototype.connect = function connect(options, callback) {
9090
? Net.createConnection(this.config.socketPath)
9191
: Net.createConnection(this.config.port, this.config.host);
9292

93+
// Connect socket to connection domain
94+
if (Events.usingDomains) {
95+
if (this._socket.domain) {
96+
this._socket.domain.remove(this._socket);
97+
}
98+
99+
if (this.domain) {
100+
this.domain.add(this._socket);
101+
}
102+
}
103+
93104
var connection = this;
94105
this._protocol.on('data', function(data) {
95106
connection._socket.write(data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var domain = null;
4+
5+
try {
6+
domain = require('domain');
7+
} catch (e) {
8+
common.skipTest('node ' + process.version + ' does not support domains');
9+
}
10+
11+
var d0 = domain.create();
12+
var d1 = domain.create();
13+
14+
var server = common.createFakeServer();
15+
16+
server.listen(common.fakeServerPort, function (err) {
17+
assert.ifError(err);
18+
19+
var connection = null;
20+
var timer = setInterval(function () {
21+
if (connection.state !== 'authenticated') return;
22+
clearInterval(timer);
23+
24+
assert.ok(!domain.active, 'no current domain');
25+
connection.query('SELECT 1', function (err) {
26+
assert.ifError(err);
27+
assert.equal(domain.active, d0, 'current domain is d0');
28+
29+
connection.destroy();
30+
server.destroy();
31+
});
32+
}, 200);
33+
34+
d0.run(function () {
35+
connection = common.createConnection({port: common.fakeServerPort});
36+
assert.equal(connection.domain, d0, 'connection belongs to d0');
37+
38+
d1.run(function () {
39+
connection.connect(function (err) {
40+
assert.ifError(err);
41+
assert.equal(domain.active, d1, 'current domain is d1');
42+
});
43+
});
44+
});
45+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var domain = null;
4+
5+
try {
6+
domain = require('domain');
7+
} catch (e) {
8+
common.skipTest('node ' + process.version + ' does not support domains');
9+
}
10+
11+
var d0 = domain.create();
12+
13+
var server = common.createFakeServer();
14+
15+
server.listen(common.fakeServerPort, function (err) {
16+
assert.ifError(err);
17+
18+
var connection = common.createConnection({port: common.fakeServerPort});
19+
var timer = setInterval(function () {
20+
if (connection.state !== 'authenticated') return;
21+
clearInterval(timer);
22+
23+
assert.ok(!domain.active, 'no current domain');
24+
connection.query('SELECT 1', function (err) {
25+
assert.ifError(err);
26+
assert.equal(domain.active, null, 'query is not bound to domain');
27+
28+
connection.destroy();
29+
server.destroy();
30+
});
31+
}, 200);
32+
33+
assert.equal(connection.domain, null, 'connection is not bound to domain');
34+
35+
d0.run(function () {
36+
connection.connect(function (err) {
37+
assert.ifError(err);
38+
assert.equal(domain.active, d0, 'current domain is d0');
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)