Skip to content

Commit 84a3f32

Browse files
committed
Merge pull request #531 from brianc/domains
domain aware connection pool
2 parents def663d + ecbc61c commit 84a3f32

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

lib/connection-parameters.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var url = require('url');
12
var dns = require('dns');
23
var path = require('path');
34

@@ -17,7 +18,6 @@ var val = function(key, config, envVar) {
1718
defaults[key];
1819
};
1920

20-
var url = require('url');
2121
//parses a connection string
2222
var parse = function(str) {
2323
var config;

lib/pool.js

+18
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,27 @@ var pools = {
5050
}
5151
//monkey-patch with connect method
5252
pool.connect = function(cb) {
53+
var domain = process.domain;
5354
pool.acquire(function(err, client) {
55+
if(domain) {
56+
cb = domain.bind(cb);
57+
domain.add(client);
58+
//native clients do not have a connection object
59+
if(client.connection) {
60+
domain.add(client.connection);
61+
domain.add(client.connection.stream);
62+
}
63+
}
5464
if(err) return cb(err, null, function() {/*NOOP*/});
5565
cb(null, client, function(err) {
66+
if(domain) {
67+
//native clients do not have a connection object
68+
if(client.connection) {
69+
domain.remove(client.connection.stream);
70+
domain.remove(client.connection);
71+
}
72+
domain.remove(client);
73+
}
5674
if(err) {
5775
pool.destroy(client);
5876
} else {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
},
2828
"devDependencies": {
2929
"jshint": "1.1.0",
30-
"semver": "~1.1.4"
30+
"semver": "~1.1.4",
31+
"async": "0.2.10"
3132
},
3233
"scripts": {
3334
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres",

test/integration/domain-tests.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var helper = require('./test-helper')
2+
var async = require('async')
3+
4+
var testWithoutDomain = function(cb) {
5+
test('no domain', function() {
6+
assert(!process.domain)
7+
helper.pg.connect(helper.config, assert.success(function(client, done) {
8+
assert(!process.domain)
9+
done()
10+
cb()
11+
}))
12+
})
13+
}
14+
15+
var testWithDomain = function(cb) {
16+
test('with domain', function() {
17+
assert(!process.domain)
18+
var domain = require('domain').create()
19+
domain.run(function() {
20+
var startingDomain = process.domain
21+
assert(startingDomain)
22+
helper.pg.connect(helper.config, assert.success(function(client, done) {
23+
assert(process.domain, 'no domain exists in connect callback')
24+
assert.equal(startingDomain, process.domain, 'domain was lost when checking out a client')
25+
var query = client.query('SELECT NOW()', assert.success(function() {
26+
assert(process.domain, 'no domain exists in query callback')
27+
assert.equal(startingDomain, process.domain, 'domain was lost when checking out a client')
28+
done(true)
29+
process.domain.exit()
30+
cb()
31+
}))
32+
}))
33+
})
34+
})
35+
}
36+
37+
var testErrorWithDomain = function(cb) {
38+
test('error on domain', function() {
39+
var domain = require('domain').create()
40+
domain.on('error', function() {
41+
cb()
42+
})
43+
domain.run(function() {
44+
helper.pg.connect(helper.config, assert.success(function(client, done) {
45+
client.query('SELECT SLDKJFLSKDJF')
46+
client.on('drain', done)
47+
}))
48+
})
49+
})
50+
}
51+
52+
async.series([
53+
testWithoutDomain,
54+
testWithDomain,
55+
testErrorWithDomain
56+
], function() {
57+
helper.pg.end()
58+
})

0 commit comments

Comments
 (0)