Skip to content

Commit

Permalink
Add size/available/pending/borrowed to ConnectionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Mar 7, 2019
1 parent a2314f2 commit fc63c24
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
51 changes: 51 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,41 @@ class ConnectionPool extends EventEmitter {
propagateCreateError: true
}, this.config.pool)
)
const self = this
Object.defineProperties(this.pool, {
size: {
get: () => {
const message = 'the `size` property on pool is deprecated, access it directly on the `ConnectionPool`'
self.emit('debug', message)
process.emitWarning(message)
return self.size
}
},
available: {
get: () => {
const message = 'the `available` property on pool is deprecated, access it directly on the `ConnectionPool`'
self.emit('debug', message)
process.emitWarning(message)
return self.available
}
},
pending: {
get: () => {
const message = 'the `pending` property on pool is deprecate, access it directly on the `ConnectionPool`'
self.emit('debug', message)
process.emitWarning(message)
return self.pending
}
},
borrowed: {
get: () => {
const message = 'the `borrowed` property on pool is deprecated, access it directly on the `ConnectionPool`'
self.emit('debug', message)
process.emitWarning(message)
return self.borrowed
}
}
})

this._connecting = false
this._connected = true
Expand All @@ -277,6 +312,22 @@ class ConnectionPool extends EventEmitter {
})
}

get size () {
return this.pool.numFree() + this.pool.numUsed() + this.pool.numPendingCreates()
}

get available () {
return this.pool.numFree()
}

get pending () {
return this.pool.numPendingAcquires()
}

get borrowed () {
return this.pool.numUsed()
}

/**
* Close all active connections in the pool.
*
Expand Down
21 changes: 12 additions & 9 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,10 @@ module.exports = (sql, driver) => {
const complete = () =>
setTimeout(() => {
// this must be delayed because destroying connection take some time
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)
assert.equal(connection.size, 3)
assert.equal(connection.available, 3)
assert.equal(connection.pending, 0)
assert.equal(connection.borrowed, 0)
done()
}, 500)

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

setImmediate(() => {
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)
assert.equal(connection.size, 1)
assert.equal(connection.available, 0)
assert.equal(connection.pending, 3)
assert.equal(connection.borrowed, 0)
})
},

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

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)
assert.equal(connection2.size, 1)
assert.equal(connection2.available, 1)
assert.equal(connection2.pending, 0)
assert.equal(connection2.borrowed, 0)

done()
})
Expand Down

0 comments on commit fc63c24

Please sign in to comment.