Skip to content

Commit

Permalink
Move Pool module to core (#1212)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bigmontz authored Aug 28, 2024
1 parent c4e4c1c commit c424636
Show file tree
Hide file tree
Showing 18 changed files with 620 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/

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

const {
pool: {
Pool, PoolConfig
}
} = internal

export default class PooledConnectionProvider extends ConnectionProvider {
constructor (
{ id, config, log, userAgent, boltAgent, authTokenManager, newPool = (...args) => new Pool(...args) },
Expand Down
1 change: 0 additions & 1 deletion packages/bolt-connection/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ export * as bolt from './bolt'
export * as buf from './buf'
export * as channel from './channel'
export * as packstream from './packstream'
export * as pool from './pool'

export * from './connection-provider'
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

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

const {
serverAddress: { ServerAddress },
logger: { Logger }
logger: { Logger },
pool: { Pool }
} = internal

describe('#unit DirectConnectionProvider', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
authTokenManagers
} from 'neo4j-driver-core'
import { RoutingTable } from '../../src/rediscovery/'
import { Pool } from '../../src/pool'
import SimpleHostNameResolver from '../../src/channel/browser/browser-host-name-resolver'
import RoutingConnectionProvider from '../../src/connection-provider/connection-provider-routing'
import { DelegateConnection, Connection } from '../../src/connection'
Expand All @@ -37,7 +36,8 @@ import LivenessCheckProvider from '../../src/connection-provider/liveness-check-

const {
serverAddress: { ServerAddress },
logger: { Logger }
logger: { Logger },
pool: { Pool }
} = internal

const { SERVICE_UNAVAILABLE, SESSION_EXPIRED } = error
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import * as serverAddress from './server-address'
import * as resolver from './resolver'
import * as objectUtil from './object-util'
import * as boltAgent from './bolt-agent/index'
import * as pool from './pool'

export {
util,
Expand All @@ -44,5 +45,6 @@ export {
serverAddress,
resolver,
objectUtil,
boltAgent
boltAgent,
pool
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,42 @@ const DEFAULT_MAX_SIZE = 100
const DEFAULT_ACQUISITION_TIMEOUT = 60 * 1000 // 60 seconds

export default class PoolConfig {
constructor (maxSize, acquisitionTimeout) {
public readonly maxSize: number
public readonly acquisitionTimeout: number

constructor (maxSize: number, acquisitionTimeout: number) {
this.maxSize = valueOrDefault(maxSize, DEFAULT_MAX_SIZE)
this.acquisitionTimeout = valueOrDefault(
acquisitionTimeout,
DEFAULT_ACQUISITION_TIMEOUT
)
}

static defaultConfig () {
static defaultConfig (): PoolConfig {
return new PoolConfig(DEFAULT_MAX_SIZE, DEFAULT_ACQUISITION_TIMEOUT)
}

static fromDriverConfig (config) {
const maxSizeConfigured = isConfigured(config.maxConnectionPoolSize)
const maxSize = maxSizeConfigured
static fromDriverConfig (config: { maxConnectionPoolSize?: number, connectionAcquisitionTimeout?: number }): PoolConfig {
const maxSize = isConfigured(config.maxConnectionPoolSize)
? config.maxConnectionPoolSize
: DEFAULT_MAX_SIZE
const acquisitionTimeoutConfigured = isConfigured(

const acquisitionTimeout = isConfigured(
config.connectionAcquisitionTimeout
)
const acquisitionTimeout = acquisitionTimeoutConfigured
? config.connectionAcquisitionTimeout
: DEFAULT_ACQUISITION_TIMEOUT

return new PoolConfig(maxSize, acquisitionTimeout)
}
}

function valueOrDefault (value, defaultValue) {
return value === 0 || value ? value : defaultValue
function valueOrDefault (value: number | undefined, defaultValue: number): number {
return isConfigured(value) ? value : defaultValue
}

function isConfigured (value) {
return value === 0 || value
function isConfigured (value?: number): value is number {
return value === 0 || value != null
}

export { DEFAULT_MAX_SIZE, DEFAULT_ACQUISITION_TIMEOUT }
Loading

0 comments on commit c424636

Please sign in to comment.