Skip to content

Commit d305b05

Browse files
authored
Merge pull request #264 from thobe/1.1-default-retry-config
Update the default routing retry config
2 parents 9dcbbf9 + da50b52 commit d305b05

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public class Config
6161
/** Strategy for how to trust encryption certificate */
6262
private final TrustStrategy trustStrategy;
6363

64-
private final int minServersInCluster;
65-
private final int maxRoutingFailures;
64+
private final int routingFailureLimit;
6665
private final long routingRetryDelayMillis;
6766

6867
private Config( ConfigBuilder builder)
@@ -74,8 +73,7 @@ private Config( ConfigBuilder builder)
7473

7574
this.encryptionLevel = builder.encryptionLevel;
7675
this.trustStrategy = builder.trustStrategy;
77-
this.minServersInCluster = builder.minServersInCluster;
78-
this.maxRoutingFailures = builder.maxRoutingFailures;
76+
this.routingFailureLimit = builder.routingFailureLimit;
7977
this.routingRetryDelayMillis = builder.routingRetryDelayMillis;
8078
}
8179

@@ -152,7 +150,7 @@ public static Config defaultConfig()
152150

153151
RoutingSettings routingSettings()
154152
{
155-
return new RoutingSettings( maxRoutingFailures, routingRetryDelayMillis );
153+
return new RoutingSettings( routingFailureLimit, routingRetryDelayMillis );
156154
}
157155

158156
/**
@@ -166,8 +164,7 @@ public static class ConfigBuilder
166164
private EncryptionLevel encryptionLevel = EncryptionLevel.REQUIRED_NON_LOCAL;
167165
private TrustStrategy trustStrategy = trustOnFirstUse(
168166
new File( getProperty( "user.home" ), ".neo4j" + File.separator + "known_hosts" ) );
169-
public int minServersInCluster = 3;
170-
private int maxRoutingFailures = 10;
167+
private int routingFailureLimit = 1;
171168
private long routingRetryDelayMillis = 5_000;
172169

173170
private ConfigBuilder() {}
@@ -279,14 +276,25 @@ public ConfigBuilder withTrustStrategy( TrustStrategy trustStrategy )
279276
* The routing servers are tried in order. If connecting any of them fails, they are all retried after
280277
* {@linkplain #withRoutingRetryDelay a delay}. This process of retrying all servers is then repeated for the
281278
* number of times specified here before considering the cluster unavailable.
279+
* <p>
280+
* The default value of this parameter is {@code 1}, which means that the the driver will not re-attempt to
281+
* connect to the cluster when connecting has failed to each individual server in the list of routers. This
282+
* default value is sensible under this assumption that if the attempt to connect fails for all servers, then
283+
* the entire cluster is down, or the client is disconnected from the network, and retrying to connect will
284+
* not bring it back up, in which case it is better to report the failure sooner.
282285
*
283-
* @param routingRetryLimit
286+
* @param routingFailureLimit
284287
* the number of times to retry each server in the list of routing servers
285288
* @return this builder
286289
*/
287-
public ConfigBuilder withRoutingRetryLimit( int routingRetryLimit )
290+
public ConfigBuilder withRoutingFailureLimit( int routingFailureLimit )
288291
{
289-
this.maxRoutingFailures = routingRetryLimit;
292+
if ( routingFailureLimit < 1 )
293+
{
294+
throw new IllegalArgumentException(
295+
"The failure limit may not be smaller than 1, but was: " + routingFailureLimit );
296+
}
297+
this.routingFailureLimit = routingFailureLimit;
290298
return this;
291299
}
292300

@@ -297,13 +305,15 @@ public ConfigBuilder withRoutingRetryLimit( int routingRetryLimit )
297305
* The delay is measured from when the first attempt to connect was made, so that the delay time specifies a
298306
* retry interval.
299307
* <p>
300-
* For each {@linkplain #withRoutingRetryLimit retry attempt} the delay time will be doubled. The time
308+
* For each {@linkplain #withRoutingFailureLimit retry attempt} the delay time will be doubled. The time
301309
* specified here is the base time, i.e. the time to wait before the first retry. If that attempt (on all
302310
* servers) also fails, the delay before the next retry will be double the time specified here, and the next
303311
* attempt after that will be double that, et.c. So if, for example, the delay specified here is
304312
* {@code 5 SECONDS}, then after attempting to connect to each server fails reconnecting will be attempted
305313
* 5 seconds after the first connection attempt to the first server. If that attempt also fails to connect to
306314
* all servers, the next attempt will start 10 seconds after the second attempt started.
315+
* <p>
316+
* The default value of this parameter is {@code 5 SECONDS}.
307317
*
308318
* @param delay
309319
* the amount of time between attempts to reconnect to the same server
@@ -313,7 +323,13 @@ public ConfigBuilder withRoutingRetryLimit( int routingRetryLimit )
313323
*/
314324
public ConfigBuilder withRoutingRetryDelay( long delay, TimeUnit unit )
315325
{
316-
this.routingRetryDelayMillis = unit.toMillis( delay );
326+
long routingRetryDelayMillis = unit.toMillis( delay );
327+
if ( routingRetryDelayMillis < 0 )
328+
{
329+
throw new IllegalArgumentException( String.format(
330+
"The retry delay may not be smaller than 0, but was %d %s.", delay, unit ) );
331+
}
332+
this.routingRetryDelayMillis = routingRetryDelayMillis;
317333
return this;
318334
}
319335

0 commit comments

Comments
 (0)