Skip to content

Commit c424636

Browse files
authored
Move Pool module to core (#1212)
The `pool` holds logic on resource pooling and it is used to avoid better resource management, enabling the driver to re-use connections and avoiding overflow server and cluster with too much connections. The pool also regulates acquisition timeouts, this avoiding clients to wait an undetermined amount time for acquiring resource when driver is busy. This module is being moved to the core module to enabling drivers developers to re-use this module in context which is not Bolt.
1 parent c4e4c1c commit c424636

File tree

18 files changed

+620
-433
lines changed

18 files changed

+620
-433
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
*/
1717

1818
import { createChannelConnection, ConnectionErrorHandler } from '../connection'
19-
import Pool, { PoolConfig } from '../pool'
20-
import { error, ConnectionProvider, ServerInfo, newError } from 'neo4j-driver-core'
19+
import { error, ConnectionProvider, ServerInfo, newError, internal } from 'neo4j-driver-core'
2120
import AuthenticationProvider from './authentication-provider'
2221
import { object } from '../lang'
2322
import LivenessCheckProvider from './liveness-check-provider'
@@ -31,6 +30,12 @@ const AUTHENTICATION_ERRORS = [
3130
'Neo.ClientError.Security.Unauthorized'
3231
]
3332

33+
const {
34+
pool: {
35+
Pool, PoolConfig
36+
}
37+
} = internal
38+
3439
export default class PooledConnectionProvider extends ConnectionProvider {
3540
constructor (
3641
{ id, config, log, userAgent, boltAgent, authTokenManager, newPool = (...args) => new Pool(...args) },

packages/bolt-connection/src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ export * as bolt from './bolt'
2020
export * as buf from './buf'
2121
export * as channel from './channel'
2222
export * as packstream from './packstream'
23-
export * as pool from './pool'
2423

2524
export * from './connection-provider'

packages/bolt-connection/test/connection-provider/connection-provider-direct.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import DirectConnectionProvider from '../../src/connection-provider/connection-provider-direct'
19-
import { Pool } from '../../src/pool'
2019
import { Connection, DelegateConnection } from '../../src/connection'
2120
import { authTokenManagers, internal, newError, ServerInfo, staticAuthTokenManager } from 'neo4j-driver-core'
2221
import AuthenticationProvider from '../../src/connection-provider/authentication-provider'
@@ -25,7 +24,8 @@ import LivenessCheckProvider from '../../src/connection-provider/liveness-check-
2524

2625
const {
2726
serverAddress: { ServerAddress },
28-
logger: { Logger }
27+
logger: { Logger },
28+
pool: { Pool }
2929
} = internal
3030

3131
describe('#unit DirectConnectionProvider', () => {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
authTokenManagers
2828
} from 'neo4j-driver-core'
2929
import { RoutingTable } from '../../src/rediscovery/'
30-
import { Pool } from '../../src/pool'
3130
import SimpleHostNameResolver from '../../src/channel/browser/browser-host-name-resolver'
3231
import RoutingConnectionProvider from '../../src/connection-provider/connection-provider-routing'
3332
import { DelegateConnection, Connection } from '../../src/connection'
@@ -37,7 +36,8 @@ import LivenessCheckProvider from '../../src/connection-provider/liveness-check-
3736

3837
const {
3938
serverAddress: { ServerAddress },
40-
logger: { Logger }
39+
logger: { Logger },
40+
pool: { Pool }
4141
} = internal
4242

4343
const { SERVICE_UNAVAILABLE, SESSION_EXPIRED } = error

packages/core/src/internal/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as serverAddress from './server-address'
2929
import * as resolver from './resolver'
3030
import * as objectUtil from './object-util'
3131
import * as boltAgent from './bolt-agent/index'
32+
import * as pool from './pool'
3233

3334
export {
3435
util,
@@ -44,5 +45,6 @@ export {
4445
serverAddress,
4546
resolver,
4647
objectUtil,
47-
boltAgent
48+
boltAgent,
49+
pool
4850
}

packages/bolt-connection/src/pool/pool-config.js renamed to packages/core/src/internal/pool/pool-config.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,42 @@ const DEFAULT_MAX_SIZE = 100
1919
const DEFAULT_ACQUISITION_TIMEOUT = 60 * 1000 // 60 seconds
2020

2121
export default class PoolConfig {
22-
constructor (maxSize, acquisitionTimeout) {
22+
public readonly maxSize: number
23+
public readonly acquisitionTimeout: number
24+
25+
constructor (maxSize: number, acquisitionTimeout: number) {
2326
this.maxSize = valueOrDefault(maxSize, DEFAULT_MAX_SIZE)
2427
this.acquisitionTimeout = valueOrDefault(
2528
acquisitionTimeout,
2629
DEFAULT_ACQUISITION_TIMEOUT
2730
)
2831
}
2932

30-
static defaultConfig () {
33+
static defaultConfig (): PoolConfig {
3134
return new PoolConfig(DEFAULT_MAX_SIZE, DEFAULT_ACQUISITION_TIMEOUT)
3235
}
3336

34-
static fromDriverConfig (config) {
35-
const maxSizeConfigured = isConfigured(config.maxConnectionPoolSize)
36-
const maxSize = maxSizeConfigured
37+
static fromDriverConfig (config: { maxConnectionPoolSize?: number, connectionAcquisitionTimeout?: number }): PoolConfig {
38+
const maxSize = isConfigured(config.maxConnectionPoolSize)
3739
? config.maxConnectionPoolSize
3840
: DEFAULT_MAX_SIZE
39-
const acquisitionTimeoutConfigured = isConfigured(
41+
42+
const acquisitionTimeout = isConfigured(
4043
config.connectionAcquisitionTimeout
4144
)
42-
const acquisitionTimeout = acquisitionTimeoutConfigured
4345
? config.connectionAcquisitionTimeout
4446
: DEFAULT_ACQUISITION_TIMEOUT
4547

4648
return new PoolConfig(maxSize, acquisitionTimeout)
4749
}
4850
}
4951

50-
function valueOrDefault (value, defaultValue) {
51-
return value === 0 || value ? value : defaultValue
52+
function valueOrDefault (value: number | undefined, defaultValue: number): number {
53+
return isConfigured(value) ? value : defaultValue
5254
}
5355

54-
function isConfigured (value) {
55-
return value === 0 || value
56+
function isConfigured (value?: number): value is number {
57+
return value === 0 || value != null
5658
}
5759

5860
export { DEFAULT_MAX_SIZE, DEFAULT_ACQUISITION_TIMEOUT }

0 commit comments

Comments
 (0)