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

Use domain.bind to preserve domain context #152

Merged
merged 1 commit into from Oct 15, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/generic-pool.js
Expand Up @@ -384,6 +384,9 @@ Pool.prototype.acquire = function acquire (callback, priority) {
if (this._draining) {
throw new Error('pool is draining and cannot accept work')
}
if (process.domain) {
callback = process.domain.bind(callback)
}
this._waitingClients.enqueue(callback, priority)
this._dispense()
return (this._count < this._factory.max)
Expand Down
46 changes: 46 additions & 0 deletions test/generic-pool.test.js
Expand Up @@ -747,5 +747,51 @@ module.exports = {
assert.equal(pool.availableObjectsCount(), 0)
assert.equal(pool.inUseObjectsCount(), 1)
})
},

'domain context is preserved on acquire callback': function (beforeExit) {
var assertion_count = 0
var pool = poolModule.Pool({
name: 'test',
create: function (cb) {
cb(null, {})
},
destroy: function (client) {},
max: 2,
idleTimeoutMillis: 1000
})

// bail on old node versions because domains didn't exist until v0.8
if (process.version < 'v0.8') {
return
}

var domain = require('domain')

function check (index) {
var wrapDomain = domain.create()
wrapDomain.index = index

wrapDomain.run(function () {
pool.acquire(function (err, client) {
assert.ifError(err)
assert.equal(domain.active.index, index)
assertion_count++
setTimeout(function () {
pool.release(client)
}, 50)
})
})
}

// first two will work even without domain binding
check(1)
check(2)
// third and on will fail without domain binding
check(3)

beforeExit(function () {
assert.equal(assertion_count, 3)
})
}
}