Skip to content

Commit 9351009

Browse files
committed
Fix parsing of connectionTimeout setting
Connection timeout limits amount of time connection spends waiting for the remote side to respond. It makes connect not hang forever if remote side does not respond. Configured positive values define the actual timeout. Zero and negative values mean infinite timeout. Previously configured value was not correctly interpreted and thus it was not possible to disable the timeout. Zero and negative values resulted in a default value of 5 seconds to be used. This commit fixes the problem so that zero and negative values mean no timeout.
1 parent 378ad92 commit 9351009

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/v1/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
144144
* // version.
145145
* loadBalancingStrategy: "least_connected" | "round_robin",
146146
*
147-
* // Specify socket connection timeout in milliseconds. Non-numeric, negative and zero values are treated as an
148-
* // infinite timeout. Connection will be then bound by the timeout configured on the operating system level.
149-
* // Timeout value should be numeric and greater or equal to zero. Default value is 5000 which is 5 seconds.
147+
* // Specify socket connection timeout in milliseconds. Numeric values are expected. Negative and zero values
148+
* // result in no timeout being applied. Connection establishment will be then bound by the timeout configured
149+
* // on the operating system level. Default value is 5000, which is 5 seconds.
150150
* connectionTimeout: 5000, // 5 seconds
151151
* }
152152
*

src/v1/internal/ch-config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ function extractKnownHostsPath(driverConfig) {
6868

6969
function extractConnectionTimeout(driverConfig) {
7070
const configuredTimeout = parseInt(driverConfig.connectionTimeout, 10);
71-
if (!configuredTimeout || configuredTimeout < 0) {
71+
if (configuredTimeout === 0) {
72+
// timeout explicitly configured to 0
73+
return null;
74+
} else if (configuredTimeout && configuredTimeout < 0) {
75+
// timeout explicitly configured to a negative value
76+
return null;
77+
} else if (!configuredTimeout) {
78+
// timeout not configured, use default value
7279
return DEFAULT_CONNECTION_TIMEOUT_MILLIS;
80+
} else {
81+
// timeout configured, use the provided value
82+
return configuredTimeout;
7383
}
74-
return configuredTimeout;
7584
}

test/internal/ch-config.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,16 @@ describe('ChannelConfig', () => {
117117
expect(config.connectionTimeout).toEqual(424242);
118118
});
119119

120+
it('should respect disabled connection timeout with value zero', () => {
121+
const config = new ChannelConfig(null, {connectionTimeout: 0}, '');
122+
123+
expect(config.connectionTimeout).toBeNull();
124+
});
125+
126+
it('should respect disabled connection timeout with negative value', () => {
127+
const config = new ChannelConfig(null, {connectionTimeout: -42}, '');
128+
129+
expect(config.connectionTimeout).toBeNull();
130+
});
131+
120132
});

0 commit comments

Comments
 (0)