Skip to content
Open
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
5 changes: 3 additions & 2 deletions packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Pool extends EventEmitter {
this.log('pulse queue ended')
return
}
if (this.ending) {
if (this.ending && !this._pendingQueue.length) {
this.log('pulse queue on ending')
if (this._idle.length) {
this._idle.slice().map((item) => {
Expand Down Expand Up @@ -389,7 +389,8 @@ class Pool extends EventEmitter {
this.emit('release', err, client)

// TODO(bmc): expose a proper, public interface _queryable and _ending
if (err || this.ending || !client._queryable || client._ending || client._poolUseCount >= this.options.maxUses) {
const draining = this.ending && !this._pendingQueue.length
if (err || draining || !client._queryable || client._ending || client._poolUseCount >= this.options.maxUses) {
if (client._poolUseCount >= this.options.maxUses) {
this.log('remove expended client')
}
Expand Down
15 changes: 15 additions & 0 deletions packages/pg-pool/test/ending.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ describe('pool ending', () => {
})
)

it('finishes queries waiting for a client when the pool ends', async () => {
const pool = new Pool({ max: 1 })
const client = await pool.connect()
const queries = [1, 2, 3].map((n) => pool.query('SELECT $1::int AS n', [n]))
const ended = pool.end()
client.release()

const results = await Promise.all(queries)
await ended

expect(results.map((x) => x.rows[0].n)).to.eql([1, 2, 3])
expect(pool.totalCount).to.equal(0)
expect(pool.ended).to.equal(true)
})

it('pool.end() - finish pending queries', async () => {
const pool = new Pool({ max: 20 })
let completed = 0
Expand Down
Loading