Skip to content

Commit d057222

Browse files
authored
Merge pull request #297 from lutovich/1.5-pool-configs
Deprecate connectionPoolSize config setting
2 parents 0e199f3 + f644ce8 commit d057222

File tree

10 files changed

+206
-73
lines changed

10 files changed

+206
-73
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_MAX_SIZE = 50;
21+
const DEFAULT_ACQUISITION_TIMEOUT = 60000;
22+
23+
export default class PoolConfig {
24+
25+
constructor(maxSize, acquisitionTimeout) {
26+
this.maxSize = valueOrDefault(maxSize, DEFAULT_MAX_SIZE);
27+
this.acquisitionTimeout = valueOrDefault(acquisitionTimeout, DEFAULT_ACQUISITION_TIMEOUT);
28+
}
29+
30+
static defaultConfig() {
31+
return new PoolConfig(DEFAULT_MAX_SIZE, DEFAULT_ACQUISITION_TIMEOUT);
32+
}
33+
34+
static fromDriverConfig(config) {
35+
const maxIdleSizeConfigured = isConfigured(config.connectionPoolSize);
36+
const maxSizeConfigured = isConfigured(config.maxConnectionPoolSize);
37+
38+
let maxSize;
39+
40+
if (maxSizeConfigured) {
41+
// correct size setting is set - use it's value
42+
maxSize = config.maxConnectionPoolSize;
43+
} else if (maxIdleSizeConfigured) {
44+
// deprecated size setting is set - use it's value
45+
console.warn('WARNING: neo4j-driver setting "connectionPoolSize" is deprecated, please use "maxConnectionPoolSize" instead');
46+
maxSize = config.connectionPoolSize;
47+
} else {
48+
maxSize = DEFAULT_MAX_SIZE;
49+
}
50+
51+
const acquisitionTimeoutConfigured = isConfigured(config.connectionAcquisitionTimeout);
52+
const acquisitionTimeout = acquisitionTimeoutConfigured ? config.connectionAcquisitionTimeout : DEFAULT_ACQUISITION_TIMEOUT;
53+
54+
return new PoolConfig(maxSize, acquisitionTimeout);
55+
}
56+
}
57+
58+
function valueOrDefault(value, defaultValue) {
59+
return value === 0 || value ? value : defaultValue;
60+
}
61+
62+
function isConfigured(value) {
63+
return value === 0 || value;
64+
}
65+
66+
export {
67+
DEFAULT_MAX_SIZE,
68+
DEFAULT_ACQUISITION_TIMEOUT
69+
};

src/v1/internal/pool.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,25 @@
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;
40-
this._maxIdleSize = config.maxIdleSize;
4139
this._maxSize = config.maxSize;
4240
this._acquisitionTimeout = config.acquisitionTimeout;
4341
this._pools = {};
@@ -151,7 +149,7 @@ class Pool {
151149

152150
if (pool) {
153151
// there exist idle connections for the given key
154-
if (pool.length >= this._maxIdleSize || !this._validate(resource)) {
152+
if (!this._validate(resource)) {
155153
this._destroy(resource);
156154
} else {
157155
pool.push(resource);

test/internal/pool-config.test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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_MAX_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.maxSize).toEqual(0);
40+
expect(config.acquisitionTimeout).toEqual(0);
41+
});
42+
43+
it('should expose default config', () => {
44+
const config = PoolConfig.defaultConfig();
45+
46+
expect(config.maxSize).toEqual(DEFAULT_MAX_SIZE);
47+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
48+
});
49+
50+
it('should convert from empty driver config', () => {
51+
const driverConfig = {};
52+
const config = PoolConfig.fromDriverConfig(driverConfig);
53+
54+
expect(config.maxSize).toEqual(DEFAULT_MAX_SIZE);
55+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
56+
});
57+
58+
it('should convert from full driver config', () => {
59+
const driverConfig = {
60+
maxConnectionPoolSize: 42,
61+
connectionAcquisitionTimeout: 4242
62+
};
63+
const config = PoolConfig.fromDriverConfig(driverConfig);
64+
65+
expect(config.maxSize).toEqual(42);
66+
expect(config.acquisitionTimeout).toEqual(4242);
67+
});
68+
69+
it('should convert from driver config with both connectionPoolSize and maxConnectionPoolSize', () => {
70+
const driverConfig = {
71+
connectionPoolSize: 42,
72+
maxConnectionPoolSize: 4242
73+
};
74+
const config = PoolConfig.fromDriverConfig(driverConfig);
75+
76+
expect(config.maxSize).toEqual(4242);
77+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
78+
});
79+
80+
it('should convert from driver config without connectionPoolSize and maxConnectionPoolSize', () => {
81+
const driverConfig = {
82+
connectionAcquisitionTimeout: 42
83+
};
84+
const config = PoolConfig.fromDriverConfig(driverConfig);
85+
86+
expect(config.maxSize).toEqual(DEFAULT_MAX_SIZE);
87+
expect(config.acquisitionTimeout).toEqual(42);
88+
});
89+
90+
it('should convert from driver config with only connectionPoolSize', () => {
91+
const driverConfig = {
92+
connectionPoolSize: 42
93+
};
94+
const config = PoolConfig.fromDriverConfig(driverConfig);
95+
96+
expect(config.maxSize).toEqual(42);
97+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
98+
});
99+
100+
it('should convert from driver config with only maxConnectionPoolSize', () => {
101+
const driverConfig = {
102+
maxConnectionPoolSize: 42
103+
};
104+
const config = PoolConfig.fromDriverConfig(driverConfig);
105+
106+
expect(config.maxSize).toEqual(42);
107+
expect(config.acquisitionTimeout).toEqual(DEFAULT_ACQUISITION_TIMEOUT);
108+
});
109+
110+
});

test/internal/pool.test.js

Lines changed: 4 additions & 49 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

@@ -103,44 +104,6 @@ describe('Pool', () => {
103104
});
104105
});
105106

106-
it('frees if pool reaches max size', (done) => {
107-
// Given a pool that tracks destroyed resources
108-
let counter = 0;
109-
let destroyed = [];
110-
const key = 'bolt://localhost:7687';
111-
const pool = new Pool(
112-
(url, release) => new Resource(url, counter++, release),
113-
resource => {
114-
destroyed.push(resource);
115-
},
116-
resource => true,
117-
{
118-
maxIdleSize: 2
119-
}
120-
);
121-
122-
// When
123-
const p0 = pool.acquire(key);
124-
const p1 = pool.acquire(key);
125-
const p2 = pool.acquire(key);
126-
127-
// Then
128-
Promise.all([ p0, p1, p2 ]).then(values => {
129-
const r0 = values[0];
130-
const r1 = values[1];
131-
const r2 = values[2];
132-
133-
r0.close();
134-
r1.close();
135-
r2.close();
136-
137-
expect(destroyed.length).toBe(1);
138-
expect(destroyed[0].id).toBe(r2.id);
139-
140-
done();
141-
});
142-
});
143-
144107
it('frees if validate returns false', (done) => {
145108
// Given a pool that allocates
146109
let counter = 0;
@@ -152,9 +115,7 @@ describe('Pool', () => {
152115
destroyed.push(resource);
153116
},
154117
resource => false,
155-
{
156-
maxIdleSize: 1000
157-
}
118+
new PoolConfig(1000, 60000)
158119
);
159120

160121
// When
@@ -440,10 +401,7 @@ describe('Pool', () => {
440401
(url, release) => new Resource(url, counter++, release),
441402
resource => {},
442403
resource => true,
443-
{
444-
maxSize: 2,
445-
acquisitionTimeout: 5000
446-
}
404+
new PoolConfig(2, 5000)
447405
);
448406

449407
const p0 = pool.acquire(key);
@@ -473,10 +431,7 @@ describe('Pool', () => {
473431
(url, release) => new Resource(url, counter++, release),
474432
resource => {},
475433
resource => true,
476-
{
477-
maxSize: 2,
478-
acquisitionTimeout: 1000
479-
}
434+
new PoolConfig(2, 1000)
480435
);
481436

482437
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) {

0 commit comments

Comments
 (0)