Skip to content

Commit

Permalink
replaces generic-pool with tarnjs
Browse files Browse the repository at this point in the history
  • Loading branch information
darryl-davidson authored and dhensby committed Feb 18, 2019
1 parent 87778c9 commit 8a8c74c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 50 deletions.
52 changes: 21 additions & 31 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const EventEmitter = require('events').EventEmitter
const debug = require('debug')('mssql:base')
const gp = require('generic-pool')
const tarn = require('tarn')

const TYPES = require('./datatypes').TYPES
const declare = require('./datatypes').declare
Expand Down Expand Up @@ -171,11 +171,11 @@ class ConnectionPool extends EventEmitter {

acquire (requester, callback) {
if (typeof callback === 'function') {
this._acquire().then(connection => callback(null, connection, this.config)).catch(callback)
this._acquire().promise.then(connection => callback(null, connection, this.config)).catch(callback)
return this
}

return this._acquire()
return this._acquire().promise
}

_acquire () {
Expand Down Expand Up @@ -244,36 +244,30 @@ class ConnectionPool extends EventEmitter {
this._poolCreate().then((connection) => {
debug('pool(%d): connected', IDS.get(this))

return this._poolDestroy(connection).then(() => {
if (!this._connecting) {
debug('pool(%d): not connecting, exiting silently (was close called before connection established?)', IDS.get(this))
return
}
this._poolDestroy(connection)
if (!this._connecting) {
debug('pool(%d): not connecting, exiting silently (was close called before connection established?)', IDS.get(this))
return
}

// prepare pool
this.pool = gp.createPool({
// prepare pool
this.pool = new tarn.Pool(
Object.assign({
create: this._poolCreate.bind(this),
validate: this._poolValidate.bind(this),
destroy: this._poolDestroy.bind(this)
}, Object.assign({
destroy: this._poolDestroy.bind(this),
max: 10,
min: 0,
evictionRunIntervalMillis: 1000,
acquireTimeoutMillis: this.config.connectionTimeout || this.config.timeout || 15000,
idleTimeoutMillis: 30000,
testOnBorrow: true
}, this.config.pool))

this.pool.on('factoryCreateError', this.emit.bind(this, 'error'))
this.pool.on('factoryDestroyError', this.emit.bind(this, 'error'))
propagateCreateError: true
}, this.config.pool)
)

this._connecting = false
this._connected = true

callback(null)
})
}).catch(err => {
this._connecting = false
callback(err)
this._connected = true

callback(null)
})
}

Expand Down Expand Up @@ -308,12 +302,8 @@ class ConnectionPool extends EventEmitter {

if (!this.pool) return setImmediate(callback, null)

const pool = this.pool
this.pool.drain().then(() => {
pool.clear()
callback(null)
})

this.pool.destroy()
callback()
this.pool = null
}

Expand Down
4 changes: 1 addition & 3 deletions lib/msnodesqlv8.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ class ConnectionPool extends base.ConnectionPool {
}

_poolValidate (tds) {
return new base.Promise((resolve, reject) => {
resolve(!tds.hasError)
})
return !tds.hasError
}

_poolDestroy (tds) {
Expand Down
4 changes: 1 addition & 3 deletions lib/tedious.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ class ConnectionPool extends base.ConnectionPool {
}

_poolValidate (tedious) {
return new base.Promise((resolve, reject) => {
resolve(!tedious.closed && !tedious.hasError)
})
return !tedious.closed && !tedious.hasError
}

_poolDestroy (tedious) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"license": "MIT",
"dependencies": {
"debug": "^3.2.6",
"generic-pool": "^3.6.1",
"tarn": "^1.1.4",
"tedious": "^4.2.0"
},
"devDependencies": {
Expand Down
21 changes: 9 additions & 12 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,9 @@ module.exports = (sql, driver) => {
const complete = () =>
setTimeout(() => {
// this must be delayed because destroying connection take some time
assert.equal(connection.pool.size, 3)
assert.equal(connection.pool.available, 3)
assert.equal(connection.pool.pending, 0)
assert.equal(connection.pool.borrowed, 0)
assert.equal(connection.pool.free.length + connection.pool.used.length + connection.pool.pendingCreates.length, 3)
assert.equal(connection.pool.free.length, 3)
assert.equal(connection.pool.pendingCreates.length, 0)
done()
}, 500)

Expand Down Expand Up @@ -874,10 +873,9 @@ module.exports = (sql, driver) => {
})

setImmediate(() => {
assert.equal(connection.pool.size, 1)
assert.equal(connection.pool.available, 0)
assert.equal(connection.pool.pending, 3)
assert.equal(connection.pool.borrowed, 0)
assert.equal(connection.pool.free.length + connection.pool.used.length + connection.pool.pendingCreates.length, 1)
assert.equal(connection.pool.free.length, 0)
assert.equal(connection.pool.pendingAcquires.length, 3)
})
},

Expand All @@ -892,10 +890,9 @@ module.exports = (sql, driver) => {
r3.query('select 1', function (err, result) {
if (err) return done(err)

assert.equal(connection2.pool.size, 1)
assert.equal(connection2.pool.available, 1)
assert.equal(connection2.pool.pending, 0)
assert.equal(connection2.pool.borrowed, 0)
assert.equal(connection2.pool.free.length + connection2.pool.used.length + connection2.pool.pendingCreates.length, 1)
assert.equal(connection2.pool.free.length, 1)
assert.equal(connection2.pool.pendingAcquires.length, 0)

done()
})
Expand Down

0 comments on commit 8a8c74c

Please sign in to comment.