Skip to content

Commit c901e6c

Browse files
authored
Fix acquisition timeout unification (#1295)
1 parent a49ea01 commit c901e6c

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

packages/bolt-connection/src/connection-provider/connection-provider-routing.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
161161
() => new RoutingTable({ database: homeDb })
162162
)
163163
if (currentRoutingTable && !currentRoutingTable.isStaleFor(accessMode)) {
164-
conn = await this.getConnectionFromRoutingTable(currentRoutingTable, auth, accessMode, databaseSpecificErrorHandler)
164+
conn = await this._getConnectionFromRoutingTable(currentRoutingTable, auth, accessMode, databaseSpecificErrorHandler)
165165
if (this.SSREnabled()) {
166166
return conn
167167
}
@@ -181,10 +181,10 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
181181
}
182182
}
183183
})
184-
return this.getConnectionFromRoutingTable(routingTable, auth, accessMode, databaseSpecificErrorHandler)
184+
return this._getConnectionFromRoutingTable(routingTable, auth, accessMode, databaseSpecificErrorHandler)
185185
}
186186

187-
async getConnectionFromRoutingTable (routingTable, auth, accessMode, databaseSpecificErrorHandler) {
187+
async _getConnectionFromRoutingTable (routingTable, auth, accessMode, databaseSpecificErrorHandler) {
188188
let name
189189
let address
190190
// select a target server based on specified access mode
@@ -290,6 +290,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
290290
}
291291

292292
async verifyAuthentication ({ database, accessMode, auth }) {
293+
this._startTime = new Date().getTime()
293294
return this._verifyAuthentication({
294295
auth,
295296
getAddress: async () => {
@@ -320,6 +321,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
320321

321322
async verifyConnectivityAndGetServerInfo ({ database, accessMode }) {
322323
const context = { database: database || DEFAULT_DB_NAME }
324+
this._startTime = new Date().getTime()
323325

324326
const routingTable = await this._freshRoutingTable({
325327
accessMode,

packages/core/src/internal/pool/pool.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import PoolConfig from './pool-config'
19-
import { newError } from '../../error'
19+
import { Neo4jError, newError } from '../../error'
2020
import { Logger } from '../logger'
2121
import { ServerAddress } from '../server-address'
2222

@@ -119,6 +119,10 @@ class Pool<R extends unknown = unknown> {
119119
const allRequests = this._acquireRequests
120120
const requests = allRequests[key]
121121
const elapsedTime = (acquisitionContext.startTime != null && acquisitionContext.startTime !== 0) ? new Date().getTime() - acquisitionContext.startTime : 0
122+
if (elapsedTime >= this._acquisitionTimeout) {
123+
throw this.getAcquisitionTimeoutError(address)
124+
}
125+
122126
if (requests == null) {
123127
allRequests[key] = []
124128
}
@@ -137,12 +141,8 @@ class Pool<R extends unknown = unknown> {
137141
// request already resolved/rejected by the release operation; nothing to do
138142
} else {
139143
// request is still pending and needs to be failed
140-
const activeCount = this.activeResourceCount(address)
141-
const idleCount = this.has(address) ? this._pools[key].length : 0
142144
request.reject(
143-
newError(
144-
`Connection acquisition timed out in ${this._acquisitionTimeout} ms. Pool status: Active conn count = ${activeCount}, Idle conn count = ${idleCount}.`
145-
)
145+
this.getAcquisitionTimeoutError(address)
146146
)
147147
}
148148
}, this._acquisitionTimeout - elapsedTime)
@@ -159,6 +159,15 @@ class Pool<R extends unknown = unknown> {
159159
})
160160
}
161161

162+
getAcquisitionTimeoutError (address: ServerAddress): Neo4jError {
163+
const key = address.asKey()
164+
const activeCount = this.activeResourceCount(address)
165+
const idleCount = this.has(address) ? this._pools[key].length : 0
166+
return newError(
167+
`Connection acquisition timed out in ${this._acquisitionTimeout} ms. Pool status: Active conn count = ${activeCount}, Idle conn count = ${idleCount}.`
168+
)
169+
}
170+
162171
/**
163172
* Destroy all idle resources for the given address.
164173
* @param {ServerAddress} address the address of the server to purge its pool.

packages/neo4j-driver-deno/lib/bolt-connection/connection-provider/connection-provider-routing.js

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/neo4j-driver-deno/lib/core/internal/pool/pool.ts

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)