Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

domain aware connection pool #531

Merged
merged 2 commits into from
Mar 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/connection-parameters.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var url = require('url');
var dns = require('dns');
var path = require('path');

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

var url = require('url');
//parses a connection string
var parse = function(str) {
var config;
Expand Down
18 changes: 18 additions & 0 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,27 @@ var pools = {
}
//monkey-patch with connect method
pool.connect = function(cb) {
var domain = process.domain;
pool.acquire(function(err, client) {
if(domain) {
cb = domain.bind(cb);
domain.add(client);
//native clients do not have a connection object
if(client.connection) {
domain.add(client.connection);
domain.add(client.connection.stream);
}
}
if(err) return cb(err, null, function() {/*NOOP*/});
cb(null, client, function(err) {
if(domain) {
//native clients do not have a connection object
if(client.connection) {
domain.remove(client.connection.stream);
domain.remove(client.connection);
}
domain.remove(client);
}
if(err) {
pool.destroy(client);
} else {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"devDependencies": {
"jshint": "1.1.0",
"semver": "~1.1.4"
"semver": "~1.1.4",
"async": "0.2.10"
},
"scripts": {
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres",
Expand Down
58 changes: 58 additions & 0 deletions test/integration/domain-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var helper = require('./test-helper')
var async = require('async')

var testWithoutDomain = function(cb) {
test('no domain', function() {
assert(!process.domain)
helper.pg.connect(helper.config, assert.success(function(client, done) {
assert(!process.domain)
done()
cb()
}))
})
}

var testWithDomain = function(cb) {
test('with domain', function() {
assert(!process.domain)
var domain = require('domain').create()
domain.run(function() {
var startingDomain = process.domain
assert(startingDomain)
helper.pg.connect(helper.config, assert.success(function(client, done) {
assert(process.domain, 'no domain exists in connect callback')
assert.equal(startingDomain, process.domain, 'domain was lost when checking out a client')
var query = client.query('SELECT NOW()', assert.success(function() {
assert(process.domain, 'no domain exists in query callback')
assert.equal(startingDomain, process.domain, 'domain was lost when checking out a client')
done(true)
process.domain.exit()
cb()
}))
}))
})
})
}

var testErrorWithDomain = function(cb) {
test('error on domain', function() {
var domain = require('domain').create()
domain.on('error', function() {
cb()
})
domain.run(function() {
helper.pg.connect(helper.config, assert.success(function(client, done) {
client.query('SELECT SLDKJFLSKDJF')
client.on('drain', done)
}))
})
})
}

async.series([
testWithoutDomain,
testWithDomain,
testErrorWithDomain
], function() {
helper.pg.end()
})