Skip to content

Commit 5c57d60

Browse files
authored
Merge pull request #428 from lutovich/1.5-default-config-values
Improve default config values for connection pool
2 parents ede1945 + f9abbe3 commit 5c57d60

File tree

3 files changed

+126
-17
lines changed

3 files changed

+126
-17
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/pool/PoolSettings.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
*/
1919
package org.neo4j.driver.internal.async.pool;
2020

21+
import java.util.concurrent.TimeUnit;
22+
2123
public class PoolSettings
2224
{
23-
public static final int NO_IDLE_CONNECTION_TEST = -1;
24-
public static final int INFINITE_CONNECTION_LIFETIME = -1;
2525
public static final int NOT_CONFIGURED = -1;
2626

27-
public static final int DEFAULT_MAX_IDLE_CONNECTION_POOL_SIZE = 10;
28-
public static final int DEFAULT_MAX_CONNECTION_POOL_SIZE = Integer.MAX_VALUE;
27+
public static final int DEFAULT_MAX_CONNECTION_POOL_SIZE = 100;
28+
public static final int DEFAULT_MAX_IDLE_CONNECTION_POOL_SIZE = DEFAULT_MAX_CONNECTION_POOL_SIZE;
2929
public static final long DEFAULT_IDLE_TIME_BEFORE_CONNECTION_TEST = NOT_CONFIGURED;
30-
public static final long DEFAULT_MAX_CONNECTION_LIFETIME = NOT_CONFIGURED;
31-
public static final long DEFAULT_CONNECTION_ACQUISITION_TIMEOUT = Long.MAX_VALUE;
30+
public static final long DEFAULT_MAX_CONNECTION_LIFETIME = TimeUnit.HOURS.toMillis( 1 );
31+
public static final long DEFAULT_CONNECTION_ACQUISITION_TIMEOUT = TimeUnit.SECONDS.toMillis( 60 );
3232

3333
private final int maxIdleConnectionPoolSize;
3434
private final long idleTimeBeforeConnectionTest;

driver/src/main/java/org/neo4j/driver/v1/Config.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import java.util.concurrent.TimeUnit;
2323
import java.util.logging.Level;
2424

25+
import org.neo4j.driver.internal.async.pool.PoolSettings;
2526
import org.neo4j.driver.internal.cluster.RoutingSettings;
2627
import org.neo4j.driver.internal.logging.JULogging;
27-
import org.neo4j.driver.internal.async.pool.PoolSettings;
2828
import org.neo4j.driver.internal.retry.RetrySettings;
2929
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
3030
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
@@ -412,7 +412,7 @@ public ConfigBuilder withConnectionLivenessCheckTimeout( long value, TimeUnit un
412412
* this case, it is recommended to set liveness check to a value smaller than network equipment has and maximum
413413
* lifetime to a reasonably large value to "renew" connections once in a while.
414414
* <p>
415-
* No maximum lifetime limit is imposed by default. Zero and negative values result in lifetime not being
415+
* Default maximum connection lifetime is 1 hour. Zero and negative values result in lifetime not being
416416
* checked.
417417
*
418418
* @param value the maximum connection lifetime
@@ -426,27 +426,64 @@ public ConfigBuilder withMaxConnectionLifetime( long value, TimeUnit unit )
426426
}
427427

428428
/**
429-
* Todo: doc and validation
429+
* Configure maximum amount of connections in the connection pool towards a single database. This setting
430+
* limits total amount of connections in the pool when used in direct driver, created for URI with 'bolt'
431+
* scheme. It will limit amount of connections per cluster member when used with routing driver, created for
432+
* URI with 'bolt+routing' scheme.
433+
* <p>
434+
* Acquisition will be attempted for at most configured timeout
435+
* {@link #withConnectionAcquisitionTimeout(long, TimeUnit)} when limit is reached.
436+
* <p>
437+
* Default value is {@code 100}. Negative values are allowed and result in unlimited pool. Value of {@code 0}
438+
* is not allowed.
430439
*
431-
* @param value
432-
* @return
440+
* @param value the maximum connection pool size.
441+
* @return this builder
442+
* @see #withConnectionAcquisitionTimeout(long, TimeUnit)
433443
*/
434444
public ConfigBuilder withMaxConnectionPoolSize( int value )
435445
{
436-
this.maxConnectionPoolSize = value;
446+
if ( value == 0 )
447+
{
448+
throw new IllegalArgumentException( "Zero value is not supported" );
449+
}
450+
else if ( value < 0 )
451+
{
452+
this.maxConnectionPoolSize = Integer.MAX_VALUE;
453+
}
454+
else
455+
{
456+
this.maxConnectionPoolSize = value;
457+
}
437458
return this;
438459
}
439460

440461
/**
441-
* Todo: doc and validation
462+
* Configure maximum amount of time connection acquisition will attempt to acquire a connection from the
463+
* connection pool. This timeout only kicks in when all existing connections are being used and no new
464+
* connections can be created because maximum connection pool size has been reached.
465+
* <p>
466+
* Exception is raised when connection can't be acquired within configured time.
467+
* <p>
468+
* Default value is 60 seconds. Negative values are allowed and result in unlimited acquisition timeout. Value
469+
* of {@code 0} is allowed and results in no timeout and immediate failure when connection is unavailable.
442470
*
443-
* @param value
444-
* @param unit
445-
* @return
471+
* @param value the acquisition timeout
472+
* @param unit the unit in which the duration is given
473+
* @return this builder
474+
* @see #withMaxConnectionPoolSize(int)
446475
*/
447476
public ConfigBuilder withConnectionAcquisitionTimeout( long value, TimeUnit unit )
448477
{
449-
this.connectionAcquisitionTimeoutMillis = unit.toMillis( value );
478+
long valueInMillis = unit.toMillis( value );
479+
if ( value >= 0 )
480+
{
481+
this.connectionAcquisitionTimeoutMillis = valueInMillis;
482+
}
483+
else
484+
{
485+
this.connectionAcquisitionTimeoutMillis = -1;
486+
}
450487
return this;
451488
}
452489

driver/src/test/java/org/neo4j/driver/v1/ConfigTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ public void shouldAllowNegativeConnectionLivenessCheckTimeout() throws Throwable
106106
assertEquals( TimeUnit.SECONDS.toMillis( -42 ), config.idleTimeBeforeConnectionTest() );
107107
}
108108

109+
@Test
110+
public void shouldHaveCorrectMaxConnectionLifetime()
111+
{
112+
assertEquals( TimeUnit.HOURS.toMillis( 1 ), Config.defaultConfig().maxConnectionLifetimeMillis() );
113+
}
114+
109115
@Test
110116
public void shouldSupportMaxConnectionLifetimeSetting() throws Throwable
111117
{
@@ -225,6 +231,72 @@ public void shouldAllowPositiveRetryAttempts()
225231
assertEquals( TimeUnit.SECONDS.toMillis( 42 ), config.retrySettings().maxRetryTimeMs() );
226232
}
227233

234+
@Test
235+
public void shouldHaveCorrectDefaultMaxConnectionPoolSize()
236+
{
237+
assertEquals( 100, Config.defaultConfig().maxConnectionPoolSize() );
238+
}
239+
240+
@Test
241+
public void shouldAllowPositiveMaxConnectionPoolSize()
242+
{
243+
Config config = Config.build().withMaxConnectionPoolSize( 42 ).toConfig();
244+
245+
assertEquals( 42, config.maxConnectionPoolSize() );
246+
}
247+
248+
@Test
249+
public void shouldAllowNegativeMaxConnectionPoolSize()
250+
{
251+
Config config = Config.build().withMaxConnectionPoolSize( -42 ).toConfig();
252+
253+
assertEquals( Integer.MAX_VALUE, config.maxConnectionPoolSize() );
254+
}
255+
256+
@Test
257+
public void shouldDisallowZeroMaxConnectionPoolSize()
258+
{
259+
try
260+
{
261+
Config.build().withMaxConnectionPoolSize( 0 ).toConfig();
262+
fail( "Exception expected" );
263+
}
264+
catch ( IllegalArgumentException e )
265+
{
266+
assertEquals( "Zero value is not supported", e.getMessage() );
267+
}
268+
}
269+
270+
@Test
271+
public void shouldHaveCorrectDefaultConnectionAcquisitionTimeout()
272+
{
273+
assertEquals( TimeUnit.SECONDS.toMillis( 60 ), Config.defaultConfig().connectionAcquisitionTimeoutMillis() );
274+
}
275+
276+
@Test
277+
public void shouldAllowPositiveConnectionAcquisitionTimeout()
278+
{
279+
Config config = Config.build().withConnectionAcquisitionTimeout( 42, TimeUnit.SECONDS ).toConfig();
280+
281+
assertEquals( TimeUnit.SECONDS.toMillis( 42 ), config.connectionAcquisitionTimeoutMillis() );
282+
}
283+
284+
@Test
285+
public void shouldAllowNegativeConnectionAcquisitionTimeout()
286+
{
287+
Config config = Config.build().withConnectionAcquisitionTimeout( -42, TimeUnit.HOURS ).toConfig();
288+
289+
assertEquals( -1, config.connectionAcquisitionTimeoutMillis() );
290+
}
291+
292+
@Test
293+
public void shouldAllowConnectionAcquisitionTimeoutOfZero()
294+
{
295+
Config config = Config.build().withConnectionAcquisitionTimeout( 0, TimeUnit.DAYS ).toConfig();
296+
297+
assertEquals( 0, config.connectionAcquisitionTimeoutMillis() );
298+
}
299+
228300
public static void deleteDefaultKnownCertFileIfExists()
229301
{
230302
if( DEFAULT_KNOWN_HOSTS.exists() )

0 commit comments

Comments
 (0)