Skip to content

Commit 1c3a7a6

Browse files
committed
Deprecate connectionPoolSize config setting
In favour of `maxConnectionPoolSize`. Extracted pool config in a dedicated class which contains logic of handling now deprecated `connectionPoolSize` and new `maxConnectionPoolSize`. Added `maxConnectionPoolSize` to TypeScript declaration. Made code use `console.warn` instead of `console.log` for deprecation warnings. Updated test neo4j version to 3.2.5 and allowed stress test run a bit longer.
1 parent 0e199f3 commit 1c3a7a6

File tree

10 files changed

+230
-36
lines changed

10 files changed

+230
-36
lines changed

src/v1/driver.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {newError, SERVICE_UNAVAILABLE} from './error';
2525
import {DirectConnectionProvider} from './internal/connection-providers';
2626
import Bookmark from './internal/bookmark';
2727
import ConnectivityVerifier from './internal/connectivity-verifier';
28+
import PoolConfig from './internal/pool-config';
2829

2930
const READ = 'READ', WRITE = 'WRITE';
3031
/**
@@ -60,11 +61,7 @@ class Driver {
6061
this._createConnection.bind(this),
6162
this._destroyConnection.bind(this),
6263
this._validateConnection.bind(this),
63-
{
64-
maxIdleSize: config.connectionPoolSize,
65-
maxSize: config.maxConnectionPoolSize,
66-
acquisitionTimeout: config.connectionAcquisitionTimeout
67-
}
64+
PoolConfig.fromDriverConfig(config)
6865
);
6966

7067
/**
@@ -243,18 +240,17 @@ class _ConnectionStreamObserver extends StreamObserver {
243240
function sanitizeConfig(config) {
244241
config.maxConnectionLifetime = sanitizeIntValue(config.maxConnectionLifetime);
245242
config.maxConnectionPoolSize = sanitizeIntValue(config.maxConnectionPoolSize);
246-
config.connectionAcquisitionTimeout = sanitizeIntValue(config.connectionAcquisitionTimeout, 60000);
243+
config.connectionAcquisitionTimeout = sanitizeIntValue(config.connectionAcquisitionTimeout);
247244
}
248245

249-
function sanitizeIntValue(value, defaultValue=null) {
246+
function sanitizeIntValue(value) {
250247
if (value) {
251248
const sanitizedValue = parseInt(value, 10);
252249
if (sanitizedValue && sanitizedValue > 0) {
253250
return sanitizedValue;
254251
}
255252
}
256-
257-
return defaultValue;
253+
return null;
258254
}
259255

260256
export {Driver, READ, WRITE}

src/v1/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
9090
* // as trusted. In the web bundle, this list of trusted certificates is maintained
9191
* // by the web browser. In NodeJS, you configure the list with the next config option.
9292
* //
93-
* // TRUST_SYSTEM_CA_SIGNED_CERTIFICATES meand that you trust whatever certificates
93+
* // TRUST_SYSTEM_CA_SIGNED_CERTIFICATES means that you trust whatever certificates
9494
* // are in the default certificate chain of th
9595
* trust: "TRUST_ALL_CERTIFICATES" | "TRUST_ON_FIRST_USE" | "TRUST_SIGNED_CERTIFICATES" |
9696
* "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES" | "TRUST_SYSTEM_CA_SIGNED_CERTIFICATES",
@@ -110,6 +110,7 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
110110
*
111111
* // The max number of connections that are allowed idle in the pool at any time.
112112
* // Connection will be destroyed if this threshold is exceeded.
113+
* // <b>Deprecated:</b> please use <code>maxConnectionPoolSize</code> instead.
113114
* connectionPoolSize: 50,
114115
*
115116
* // The maximum total number of connections allowed to be managed by the connection pool, per host.

src/v1/internal/ch-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const TrustStrategy = {
108108
* @deprecated Since version 1.0. Will be deleted in a future version. {@link #TRUST_CUSTOM_CA_SIGNED_CERTIFICATES}.
109109
*/
110110
TRUST_SIGNED_CERTIFICATES: function( config, onSuccess, onFailure ) {
111-
console.log("`TRUST_SIGNED_CERTIFICATES` has been deprecated as option and will be removed in a future version of " +
111+
console.warn('`TRUST_SIGNED_CERTIFICATES` has been deprecated as option and will be removed in a future version of ' +
112112
"the driver. Please use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead.");
113113
return TrustStrategy.TRUST_CUSTOM_CA_SIGNED_CERTIFICATES(config, onSuccess, onFailure);
114114
},
@@ -172,7 +172,7 @@ const TrustStrategy = {
172172
* @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
173173
*/
174174
TRUST_ON_FIRST_USE : function( config, onSuccess, onFailure ) {
175-
console.log("`TRUST_ON_FIRST_USE` has been deprecated as option and will be removed in a future version of " +
175+
console.warn('`TRUST_ON_FIRST_USE` has been deprecated as option and will be removed in a future version of ' +
176176
"the driver. Please use `TRUST_ALL_CERTIFICATES` instead.");
177177

178178
let tlsOpts = {

src/v1/internal/pool-config.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright (c) 2002-2017 "Neo Technology,","
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
const DEFAULT_SIZE = 50;
21+
const DEFAULT_ACQUISITION_TIMEOUT = 60000;
22+
23+
export default class PoolConfig {
24+
25+
constructor(maxIdleSize, maxSize, acquisitionTimeout) {
26+
this.maxIdleSize = valueOrDefault(maxIdleSize, DEFAULT_SIZE);
27+
this.maxSize = valueOrDefault(maxSize, DEFAULT_SIZE);
28+
this.acquisitionTimeout = valueOrDefault(acquisitionTimeout, DEFAULT_ACQUISITION_TIMEOUT);
29+
}
30+
31+
static defaultConfig() {
32+
return new PoolConfig(DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_ACQUISITION_TIMEOUT);
33+
}
34+
35+
static fromDriverConfig(config) {
36+
const maxIdleSizeConfigured = isConfigured(config.connectionPoolSize);
37+
const maxSizeConfigured = isConfigured(config.maxConnectionPoolSize);
38+
39+
if (maxIdleSizeConfigured) {
40+
console.warn('WARNING: neo4j-driver setting "connectionPoolSize" is deprecated, please use "maxConnectionPoolSize" instead');
41+
}
42+
43+
let maxIdleSize;
44+
let maxSize;
45+
46+
if (maxIdleSizeConfigured && maxSizeConfigured) {
47+
// both settings are configured - use configured values
48+
maxIdleSize = config.connectionPoolSize;
49+
maxSize = config.maxConnectionPoolSize;
50+
} else if (!maxIdleSizeConfigured && maxSizeConfigured) {
51+
// only maxSize is configured - use it's value for both
52+
maxIdleSize = config.maxConnectionPoolSize;
53+
maxSize = config.maxConnectionPoolSize;
54+
} else if (maxIdleSizeConfigured && !maxSizeConfigured) {
55+
// only maxIdleSize is configured - use it's value for both
56+
maxIdleSize = config.connectionPoolSize;
57+
maxSize = config.connectionPoolSize;
58+
} else {
59+
// none configured - use default values
60+
maxIdleSize = DEFAULT_SIZE;
61+
maxSize = DEFAULT_SIZE;
62+
}
63+
64+
const acquisitionTimeoutConfigured = isConfigured(config.connectionAcquisitionTimeout);
65+
const acquisitionTimeout = acquisitionTimeoutConfigured ? config.connectionAcquisitionTimeout : DEFAULT_ACQUISITION_TIMEOUT;
66+
67+
return new PoolConfig(maxIdleSize, maxSize, acquisitionTimeout);
68+
}
69+
}
70+
71+
function valueOrDefault(value, defaultValue) {
72+
return value === 0 || value ? value : defaultValue;
73+
}
74+
75+
function isConfigured(value) {
76+
return value === 0 || value;
77+
}
78+
79+
export {
80+
DEFAULT_SIZE,
81+
DEFAULT_ACQUISITION_TIMEOUT
82+
};

src/v1/internal/pool.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717
* limitations under the License.
1818
*/
1919

20-
import { newError } from "../error";
21-
import { promiseOrTimeout } from "./util";
20+
import {promiseOrTimeout} from './util';
21+
import PoolConfig from './pool-config';
2222

2323
class Pool {
2424
/**
25-
* @param create an allocation function that creates a new resource. It's given
25+
* @param {function} create an allocation function that creates a new resource. It's given
2626
* a single argument, a function that will return the resource to
2727
* the pool if invoked, which is meant to be called on .dispose
2828
* or .close or whatever mechanism the resource uses to finalize.
29-
* @param destroy called with the resource when it is evicted from this pool
30-
* @param validate called at various times (like when an instance is acquired and
29+
* @param {function} destroy called with the resource when it is evicted from this pool
30+
* @param {function} validate called at various times (like when an instance is acquired and
3131
* when it is returned). If this returns false, the resource will
3232
* be evicted
33-
* @param maxIdle the max number of resources that are allowed idle in the pool at
34-
* any time. If this threshold is exceeded, resources will be evicted.
33+
* @param {PoolConfig} config configuration for the new driver.
3534
*/
36-
constructor(create, destroy=(()=>true), validate=(()=>true), config={}) {
35+
constructor(create, destroy = (() => true), validate = (() => true), config = PoolConfig.defaultConfig()) {
3736
this._create = create;
3837
this._destroy = destroy;
3938
this._validate = validate;

test/internal/pool-config.test.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Copyright (c) 2002-2017 "Neo Technology,","
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import PoolConfig, {DEFAULT_ACQUISITION_TIMEOUT, DEFAULT_SIZE} from '../../src/v1/internal/pool-config';
21+
22+
describe('PoolConfig', () => {
23+
24+
let originalConsoleWarn;
25+
26+
beforeAll(() => {
27+
originalConsoleWarn = console.warn;
28+
console.warn = () => {
29+
};
30+
});
31+
32+
afterAll(() => {
33+
console.warn = originalConsoleWarn;
34+
});
35+
36+
it('should respect zero values', () => {
37+
const config = new PoolConfig(0, 0, 0);
38+
39+
expect(config.maxIdleSize).toEqual(0);
40+
expect(config.maxSize).toEqual(0);
41+
expect(config.acquisitionTimeout).toEqual(0);
42+
});
43+
44+
it('should expose default config', () => {
45+
const config = PoolConfig.defaultConfig();
46+
47+
expect(config.maxIdleSize).toEqual(DEFAULT_SIZE);
48+
expect(config.maxSize).toEqual(DEFAULT_SIZE);
49+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
50+
});
51+
52+
it('should convert from empty driver config', () => {
53+
const driverConfig = {};
54+
const config = PoolConfig.fromDriverConfig(driverConfig);
55+
56+
expect(config.maxIdleSize).toEqual(DEFAULT_SIZE);
57+
expect(config.maxSize).toEqual(DEFAULT_SIZE);
58+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
59+
});
60+
61+
it('should convert from full driver config', () => {
62+
const driverConfig = {
63+
connectionPoolSize: 42,
64+
maxConnectionPoolSize: 4242,
65+
connectionAcquisitionTimeout: 424242
66+
};
67+
const config = PoolConfig.fromDriverConfig(driverConfig);
68+
69+
expect(config.maxIdleSize).toEqual(42);
70+
expect(config.maxSize).toEqual(4242);
71+
expect(config.acquisitionTimeout).toEqual(424242);
72+
});
73+
74+
it('should convert from driver config with both connectionPoolSize and maxConnectionPoolSize', () => {
75+
const driverConfig = {
76+
connectionPoolSize: 42,
77+
maxConnectionPoolSize: 4242
78+
};
79+
const config = PoolConfig.fromDriverConfig(driverConfig);
80+
81+
expect(config.maxIdleSize).toEqual(42);
82+
expect(config.maxSize).toEqual(4242);
83+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
84+
});
85+
86+
it('should convert from driver config without connectionPoolSize and maxConnectionPoolSize', () => {
87+
const driverConfig = {
88+
connectionAcquisitionTimeout: 42
89+
};
90+
const config = PoolConfig.fromDriverConfig(driverConfig);
91+
92+
expect(config.maxIdleSize).toEqual(DEFAULT_SIZE);
93+
expect(config.maxSize).toEqual(DEFAULT_SIZE);
94+
expect(config.acquisitionTimeout).toEqual(42);
95+
});
96+
97+
it('should convert from driver config with only connectionPoolSize', () => {
98+
const driverConfig = {
99+
connectionPoolSize: 42
100+
};
101+
const config = PoolConfig.fromDriverConfig(driverConfig);
102+
103+
// both connectionPoolSize and maxConnectionPoolSize should be set to the same value
104+
expect(config.maxIdleSize).toEqual(42);
105+
expect(config.maxSize).toEqual(42);
106+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
107+
});
108+
109+
it('should convert from driver config with only maxConnectionPoolSize', () => {
110+
const driverConfig = {
111+
maxConnectionPoolSize: 42
112+
};
113+
const config = PoolConfig.fromDriverConfig(driverConfig);
114+
115+
// both connectionPoolSize and maxConnectionPoolSize should be set to the same value
116+
expect(config.maxIdleSize).toEqual(42);
117+
expect(config.maxSize).toEqual(42);
118+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
119+
});
120+
121+
});

test/internal/pool.test.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import Pool from '../../src/v1/internal/pool';
21+
import PoolConfig from '../../src/v1/internal/pool-config';
2122

2223
describe('Pool', () => {
2324

@@ -114,9 +115,7 @@ describe('Pool', () => {
114115
destroyed.push(resource);
115116
},
116117
resource => true,
117-
{
118-
maxIdleSize: 2
119-
}
118+
new PoolConfig(2, 100, 60000)
120119
);
121120

122121
// When
@@ -152,9 +151,7 @@ describe('Pool', () => {
152151
destroyed.push(resource);
153152
},
154153
resource => false,
155-
{
156-
maxIdleSize: 1000
157-
}
154+
new PoolConfig(1000, 1000, 60000)
158155
);
159156

160157
// When
@@ -440,10 +437,7 @@ describe('Pool', () => {
440437
(url, release) => new Resource(url, counter++, release),
441438
resource => {},
442439
resource => true,
443-
{
444-
maxSize: 2,
445-
acquisitionTimeout: 5000
446-
}
440+
new PoolConfig(2, 2, 5000)
447441
);
448442

449443
const p0 = pool.acquire(key);
@@ -473,10 +467,7 @@ describe('Pool', () => {
473467
(url, release) => new Resource(url, counter++, release),
474468
resource => {},
475469
resource => true,
476-
{
477-
maxSize: 2,
478-
acquisitionTimeout: 1000
479-
}
470+
new PoolConfig(2, 2, 1000)
480471
);
481472

482473
const p0 = pool.acquire(key);

test/internal/shared-neo4j.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const password = 'password';
9595
const authToken = neo4j.auth.basic(username, password);
9696

9797
const neoCtrlVersionParam = '-e';
98-
const defaultNeo4jVersion = '3.2.0';
98+
const defaultNeo4jVersion = '3.2.5';
9999
const defaultNeoCtrlArgs = `${neoCtrlVersionParam} ${defaultNeo4jVersion}`;
100100

101101
function neo4jCertPath(dir) {

test/v1/stress.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('stress tests', () => {
3030
fast: {
3131
commandsCount: 5000,
3232
parallelism: 8,
33-
maxRunTimeMs: 120000 // 2 minutes
33+
maxRunTimeMs: 180000 // 3 minutes
3434
},
3535
extended: {
3636
commandsCount: 2000000,

types/v1/driver.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ declare interface Config {
4545
trust?: TrustStrategy;
4646
trustedCertificates?: string[];
4747
knownHosts?: string;
48+
/**
49+
* @deprecated use {@link maxConnectionPoolSize} instead.
50+
*/
4851
connectionPoolSize?: number;
52+
maxConnectionPoolSize?: number;
4953
maxTransactionRetryTime?: number;
5054
loadBalancingStrategy?: LoadBalancingStrategy;
5155
maxConnectionLifetime?: number;

0 commit comments

Comments
 (0)