Skip to content

Commit 5f23abf

Browse files
committed
JAVA-398: Configure max connection retry time
1 parent e05cfb4 commit 5f23abf

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/main/com/mongodb/DBPort.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ boolean _open()
193193

194194
long sleepTime = 100;
195195

196+
long maxAutoConnectRetryTime = CONN_RETRY_TIME_MS;
197+
if (_options.maxAutoConnectRetryTime > 0) {
198+
maxAutoConnectRetryTime = _options.maxAutoConnectRetryTime;
199+
}
200+
196201
final long start = System.currentTimeMillis();
197202
while ( true ){
198203

@@ -220,11 +225,11 @@ boolean _open()
220225

221226
long sleptSoFar = System.currentTimeMillis() - start;
222227

223-
if ( sleptSoFar >= CONN_RETRY_TIME_MS )
228+
if ( sleptSoFar >= maxAutoConnectRetryTime )
224229
throw lastError;
225230

226-
if ( sleepTime + sleptSoFar > CONN_RETRY_TIME_MS )
227-
sleepTime = CONN_RETRY_TIME_MS - sleptSoFar;
231+
if ( sleepTime + sleptSoFar > maxAutoConnectRetryTime )
232+
sleepTime = maxAutoConnectRetryTime - sleptSoFar;
228233

229234
_logger.severe( "going to sleep and retry. total sleep time after = " + ( sleptSoFar + sleptSoFar ) + "ms this time:" + sleepTime + "ms" );
230235
ThreadUtil.sleep( sleepTime );

src/main/com/mongodb/MongoOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void reset(){
3737
socketTimeout = 0;
3838
socketKeepAlive = false;
3939
autoConnectRetry = false;
40+
maxAutoConnectRetryTime = 0;
4041
slaveOk = false;
4142
safe = false;
4243
w = 0;
@@ -124,6 +125,12 @@ else if (safe)
124125
*/
125126
public boolean autoConnectRetry;
126127

128+
/**
129+
* The maximum amount of time in MS to spend retrying to open connection to the same server.
130+
* Default is 0, which means to use the default 15s if autoConnectRetry is on.
131+
*/
132+
public long maxAutoConnectRetryTime;
133+
127134
/**
128135
* This flag specifies if the driver is allowed to read from secondary (slave) servers.
129136
* Specifically in the current implementation, the driver will avoid reading from the primary server and round robin requests to secondaries.
@@ -185,6 +192,7 @@ public String toString(){
185192
buf.append( "socketTimeout=" ).append( socketTimeout ).append( ", " );
186193
buf.append( "socketKeepAlive=" ).append( socketKeepAlive ).append( ", " );
187194
buf.append( "autoConnectRetry=" ).append( autoConnectRetry ).append( ", " );
195+
buf.append( "maxAutoConnectRetryTime=" ).append( maxAutoConnectRetryTime ).append( ", " );
188196
buf.append( "slaveOk=" ).append( slaveOk ).append( ", " );
189197
buf.append( "safe=" ).append( safe ).append( ", " );
190198
buf.append( "w=" ).append( w ).append( ", " );

0 commit comments

Comments
 (0)