From 779c8064f2873b190d6f671b6ecef75852688526 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Sat, 15 Mar 2014 16:40:45 -0500 Subject: [PATCH 1/2] Add domain support to connection pool --- lib/connection-parameters.js | 2 +- lib/pool.js | 18 ++++++++++ package.json | 3 +- test/integration/domain-tests.js | 58 ++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/integration/domain-tests.js diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index cf78661ab..933da76de 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -1,3 +1,4 @@ +var url = require('url'); var dns = require('dns'); var path = require('path'); @@ -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; diff --git a/lib/pool.js b/lib/pool.js index 9cf9aabf0..b461b61eb 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -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 { diff --git a/package.json b/package.json index 81ee346b4..8dd82968e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/integration/domain-tests.js b/test/integration/domain-tests.js new file mode 100644 index 000000000..a51f13dda --- /dev/null +++ b/test/integration/domain-tests.js @@ -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() +}) From ecbc61c2bc1b2d5563baea7c01fdd1755a15eafd Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Sat, 15 Mar 2014 16:46:57 -0500 Subject: [PATCH 2/2] Fix async version for older nodes --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8dd82968e..448095a3f 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "devDependencies": { "jshint": "1.1.0", "semver": "~1.1.4", - "async": "^0.2.10" + "async": "0.2.10" }, "scripts": { "test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres",