Skip to content

Commit 0ad1565

Browse files
committed
JAVA-1285: Await the minimum of the connect retry frequency and the remaining wait time
1 parent 5bcd2f8 commit 0ad1565

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/main/com/mongodb/BaseCluster.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ public Server getServer(final ServerSelector serverSelector, final long maxWaitT
6666
CountDownLatch currentPhase = phase.get();
6767
ClusterDescription curDescription = description;
6868
List<ServerDescription> serverDescriptions = serverSelector.choose(curDescription);
69-
final long endTime = System.nanoTime() + NANOSECONDS.convert(maxWaitTime, timeUnit);
69+
7070
boolean selectionFailureLogged = false;
71+
72+
final long startTimeNanos = System.nanoTime();
73+
final long endTimeNanos = startTimeNanos + NANOSECONDS.convert(maxWaitTime, timeUnit);
74+
long curTimeNanos = startTimeNanos;
75+
7176
while (true) {
7277
throwIfIncompatible(curDescription);
7378

@@ -78,7 +83,7 @@ public Server getServer(final ServerSelector serverSelector, final long maxWaitT
7883
}
7984
}
8085

81-
if (System.nanoTime() > endTime) {
86+
if (curTimeNanos > endTimeNanos) {
8287
throw new MongoTimeoutException(format("Timed out while waiting for a server that matches %s after %d ms",
8388
serverSelector, MILLISECONDS.convert(maxWaitTime, timeUnit)));
8489
}
@@ -91,7 +96,11 @@ public Server getServer(final ServerSelector serverSelector, final long maxWaitT
9196

9297
connect();
9398

94-
currentPhase.await(serverFactory.getSettings().getHeartbeatConnectRetryFrequency(NANOSECONDS), NANOSECONDS);
99+
currentPhase.await(Math.min(endTimeNanos - curTimeNanos,
100+
serverFactory.getSettings().getHeartbeatConnectRetryFrequency(NANOSECONDS)),
101+
NANOSECONDS);
102+
103+
curTimeNanos = System.nanoTime();
95104

96105
currentPhase = phase.get();
97106
curDescription = description;
@@ -109,11 +118,16 @@ public ClusterDescription getDescription(final long maxWaitTime, final TimeUnit
109118
try {
110119
CountDownLatch currentPhase = phase.get();
111120
ClusterDescription curDescription = description;
112-
final long endTime = System.nanoTime() + NANOSECONDS.convert(maxWaitTime, timeUnit);
121+
113122
boolean selectionFailureLogged = false;
123+
124+
final long startTimeNanos = System.nanoTime();
125+
final long endTimeNanos = startTimeNanos + NANOSECONDS.convert(maxWaitTime, timeUnit);
126+
long curTimeNanos = startTimeNanos;
127+
114128
while (curDescription.getType() == ClusterType.Unknown) {
115129

116-
if (System.nanoTime() > endTime) {
130+
if (curTimeNanos > endTimeNanos) {
117131
throw new MongoTimeoutException(format("Timed out while waiting to connect after %d ms",
118132
MILLISECONDS.convert(maxWaitTime, timeUnit)));
119133
}
@@ -126,7 +140,11 @@ public ClusterDescription getDescription(final long maxWaitTime, final TimeUnit
126140

127141
connect();
128142

129-
currentPhase.await(serverFactory.getSettings().getHeartbeatConnectRetryFrequency(NANOSECONDS), NANOSECONDS);
143+
currentPhase.await(Math.min(endTimeNanos - curTimeNanos,
144+
serverFactory.getSettings().getHeartbeatConnectRetryFrequency(NANOSECONDS)),
145+
NANOSECONDS);
146+
147+
curTimeNanos = System.nanoTime();
130148

131149
currentPhase = phase.get();
132150
curDescription = description;

0 commit comments

Comments
 (0)