Skip to content

Fixed a bug where connection pool size can go beyond maximum #500

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

Merged
merged 1 commit into from
Nov 25, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/v1/internal/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ class Pool {
const key = address.asKey()

if (resource) {
resourceAcquired(key, this._activeResourceCounts)
if (this._log.isDebugEnabled()) {
this._log.debug(`${resource} acquired from the pool ${key}`)
if (
this._maxSize &&
this.activeResourceCount(address) >= this._maxSize
) {
this._destroy(resource)
} else {
resourceAcquired(key, this._activeResourceCounts)
if (this._log.isDebugEnabled()) {
this._log.debug(`${resource} acquired from the pool ${key}`)
}
return resource
}
return resource
}

// We're out of resources and will try to acquire later on when an existing resource is released.
Expand All @@ -100,11 +107,11 @@ class Pool {
// request already resolved/rejected by the release operation; nothing to do
} else {
// request is still pending and needs to be failed
const activeCount = this.activeResourceCount(address)
const idleCount = this.has(address) ? this._pools[key].length : 0
request.reject(
newError(
`Connection acquisition timed out in ${
this._acquisitionTimeout
} ms.`
`Connection acquisition timed out in ${this._acquisitionTimeout} ms. Poos status: Active conn count = ${activeCount}, Idle conn count = ${idleCount}.`
)
)
}
Expand Down Expand Up @@ -209,7 +216,10 @@ class Pool {
}
if (this._installIdleObserver) {
this._installIdleObserver(resource, {
onError: () => {
onError: error => {
this._log.debug(
`Idle connection ${resource} destroyed because of error: ${error}`
)
const pool = this._pools[key]
if (pool) {
this._pools[key] = pool.filter(r => r !== resource)
Expand Down
31 changes: 31 additions & 0 deletions test/v1/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ describe('driver', () => {
}
})

it('should not decrease active connection count after driver close', done => {
// Given
const config = {
maxConnectionPoolSize: 2,
connectionAcquisitionTimeout: 0,
encrypted: false
}
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, config)

function beginTxWithoutCommit (driver) {
const session = driver.session()
const tx = session.beginTransaction()
return tx.run('RETURN 1')
}
// When

const result1 = beginTxWithoutCommit(driver)
const result2 = beginTxWithoutCommit(driver)

Promise.all([result1, result2]).then(results => {
driver.close()
beginTxWithoutCommit(driver).catch(() => {
var serverKey = Object.keys(driver._pool._activeResourceCounts)[0]
expect(driver._pool._activeResourceCounts[serverKey]).toEqual(2)
expect(driver._pool._pools[serverKey].length).toEqual(0)
expect(Object.keys(driver._openConnections).length).toEqual(2)
done()
})
})
}, 10000)

it('should expose sessions', () => {
// Given
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)
Expand Down