Skip to content

Commit 7dc7ea3

Browse files
authored
Merge pull request #299 from zhenlineo/1.5-examples
Add examples for driver documentation
2 parents d057222 + 8d36633 commit 7dc7ea3

File tree

5 files changed

+68
-32
lines changed

5 files changed

+68
-32
lines changed

src/v1/driver.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ 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';
28+
import PoolConfig, {DEFAULT_ACQUISITION_TIMEOUT, DEFAULT_MAX_SIZE} from './internal/pool-config';
29+
30+
const DEFAULT_MAX_CONNECTION_LIFETIME = 60 * 60 * 1000; // 1 hour
2931

3032
const READ = 'READ', WRITE = 'WRITE';
3133
/**
@@ -115,14 +117,8 @@ class Driver {
115117
}
116118

117119
const maxConnectionLifetime = this._config.maxConnectionLifetime;
118-
if (maxConnectionLifetime) {
119-
const lifetime = Date.now() - conn.creationTimestamp;
120-
if (lifetime > maxConnectionLifetime) {
121-
return false;
122-
}
123-
}
124-
125-
return true;
120+
const lifetime = Date.now() - conn.creationTimestamp;
121+
return lifetime <= maxConnectionLifetime;
126122
}
127123

128124
/**
@@ -238,19 +234,20 @@ class _ConnectionStreamObserver extends StreamObserver {
238234
* @private
239235
*/
240236
function sanitizeConfig(config) {
241-
config.maxConnectionLifetime = sanitizeIntValue(config.maxConnectionLifetime);
242-
config.maxConnectionPoolSize = sanitizeIntValue(config.maxConnectionPoolSize);
243-
config.connectionAcquisitionTimeout = sanitizeIntValue(config.connectionAcquisitionTimeout);
237+
config.maxConnectionLifetime = sanitizeIntValue(config.maxConnectionLifetime, DEFAULT_MAX_CONNECTION_LIFETIME);
238+
config.maxConnectionPoolSize = sanitizeIntValue(config.maxConnectionPoolSize, DEFAULT_MAX_SIZE);
239+
config.connectionAcquisitionTimeout = sanitizeIntValue(config.connectionAcquisitionTimeout, DEFAULT_ACQUISITION_TIMEOUT);
244240
}
245241

246-
function sanitizeIntValue(value) {
247-
if (value) {
248-
const sanitizedValue = parseInt(value, 10);
249-
if (sanitizedValue && sanitizedValue > 0) {
250-
return sanitizedValue;
251-
}
242+
function sanitizeIntValue(rawValue, defaultWhenAbsent) {
243+
const sanitizedValue = parseInt(rawValue, 10);
244+
if (sanitizedValue > 0 || sanitizedValue === 0) {
245+
return sanitizedValue;
246+
} else if (sanitizedValue < 0) {
247+
return Number.MAX_SAFE_INTEGER;
248+
} else {
249+
return defaultWhenAbsent;
252250
}
253-
return null;
254251
}
255252

256253
export {Driver, READ, WRITE}

src/v1/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
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.
113113
* // <b>Deprecated:</b> please use <code>maxConnectionPoolSize</code> instead.
114-
* connectionPoolSize: 50,
114+
* connectionPoolSize: 100,
115115
*
116116
* // The maximum total number of connections allowed to be managed by the connection pool, per host.
117117
* // This includes both in-use and idle connections. No maximum connection pool size is imposed
@@ -125,7 +125,7 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
125125
* // to a slightly smaller value than the one configured in network equipment (load balancer, proxy, firewall,
126126
* // etc. can also limit maximum connection lifetime). No maximum lifetime limit is imposed by default. Zero
127127
* // and negative values result in lifetime not being checked.
128-
* maxConnectionLifetime: 30 * 60 * 1000, // 30 minutes
128+
* maxConnectionLifetime: 60 * 60 * 1000, // 1 hour
129129
*
130130
* // The maximum amount of time to wait to acquire a connection from the pool (to either create a new
131131
* // connection or borrow an existing one.

src/v1/internal/pool-config.js

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

20-
const DEFAULT_MAX_SIZE = 50;
21-
const DEFAULT_ACQUISITION_TIMEOUT = 60000;
20+
const DEFAULT_MAX_SIZE = 100;
21+
const DEFAULT_ACQUISITION_TIMEOUT = 60 * 1000; // 60 seconds
2222

2323
export default class PoolConfig {
2424

test/v1/driver.test.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import neo4j from '../../src/v1';
2121
import sharedNeo4j from '../internal/shared-neo4j';
2222
import FakeConnection from '../internal/fake-connection';
2323
import lolex from 'lolex';
24+
import {DEFAULT_ACQUISITION_TIMEOUT, DEFAULT_MAX_SIZE} from '../../src/v1/internal/pool-config';
2425

2526
describe('driver', () => {
2627

@@ -191,13 +192,10 @@ describe('driver', () => {
191192
expect(() => neo4j.driver('bolt://localhost:7687/?policy=my_policy')).toThrow();
192193
});
193194

194-
it('should sanitize maxConnectionLifetime in the config', () => {
195-
validateMaxConnectionLifetime({}, null);
196-
validateMaxConnectionLifetime({maxConnectionLifetime: 42}, 42);
197-
validateMaxConnectionLifetime({maxConnectionLifetime: 0}, null);
198-
validateMaxConnectionLifetime({maxConnectionLifetime: '42'}, 42);
199-
validateMaxConnectionLifetime({maxConnectionLifetime: '042'}, 42);
200-
validateMaxConnectionLifetime({maxConnectionLifetime: -42}, null);
195+
it('should sanitize pool setting values in the config', () => {
196+
testConfigSanitizing('maxConnectionLifetime', 60 * 60 * 1000);
197+
testConfigSanitizing('maxConnectionPoolSize', DEFAULT_MAX_SIZE);
198+
testConfigSanitizing('connectionAcquisitionTimeout', DEFAULT_ACQUISITION_TIMEOUT);
201199
});
202200

203201
it('should treat closed connections as invalid', () => {
@@ -321,10 +319,19 @@ describe('driver', () => {
321319
return neo4j.auth.basic('neo4j', 'who would use such a password');
322320
}
323321

324-
function validateMaxConnectionLifetime(config, expectedValue) {
322+
function testConfigSanitizing(configProperty, defaultValue) {
323+
validateConfigSanitizing({}, defaultValue);
324+
validateConfigSanitizing({[configProperty]: 42}, 42);
325+
validateConfigSanitizing({[configProperty]: 0}, 0);
326+
validateConfigSanitizing({[configProperty]: '42'}, 42);
327+
validateConfigSanitizing({[configProperty]: '042'}, 42);
328+
validateConfigSanitizing({[configProperty]: -42}, Number.MAX_SAFE_INTEGER);
329+
}
330+
331+
function validateConfigSanitizing(config, configProperty, expectedValue) {
325332
const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, config);
326333
try {
327-
expect(driver._config.maxConnectionLifetime).toEqual(expectedValue);
334+
expect(driver._config[configProperty]).toEqual(expectedValue);
328335
} finally {
329336
driver.close();
330337
}

test/v1/examples.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,38 @@ describe('examples', () => {
107107
};
108108
});
109109

110+
it('config connection pool example', done => {
111+
// tag::config-connection-pool[]
112+
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
113+
{
114+
maxConnectionLifetime: 30*60*60,
115+
maxConnectionPoolSize: 50,
116+
connectionAcquisitionTimeout: 2*60
117+
}
118+
);
119+
// end::config-connection-pool[]
120+
121+
driver.onCompleted = () => {
122+
driver.close();
123+
done();
124+
};
125+
});
126+
127+
it('config load balancing example', done => {
128+
// tag::config-load-balancing-strategy[]
129+
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
130+
{
131+
loadBalancingStrategy: "least_connected"
132+
}
133+
);
134+
// end::config-load-balancing-strategy[]
135+
136+
driver.onCompleted = () => {
137+
driver.close();
138+
done();
139+
};
140+
});
141+
110142
it('config max retry time example', done => {
111143
// tag::config-max-retry-time[]
112144
const maxRetryTimeMs = 15 * 1000; // 15 seconds

0 commit comments

Comments
 (0)