Skip to content

Commit 59b61bc

Browse files
rozzastIncMale
authored andcommitted
Ensure callback using cluster description is guarded (#878)
Getting the description from a cluster can throw a MongoTimeoutException or a MongoInterruptedException, so in that case the exception should be passed to the callback. JAVA-4432 Co-authored-by: Valentin Kovalenko <[email protected]>
1 parent 66920d1 commit 59b61bc

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

driver-core/src/main/com/mongodb/internal/async/client/ClientSessionBinding.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,27 @@ public ReadPreference getReadPreference() {
5858

5959
@Override
6060
public void getReadConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
61-
if (isConnectionSourcePinningRequired()) {
62-
getPinnedConnectionSource(true, callback);
63-
} else {
64-
wrapped.getReadConnectionSource(new WrappingCallback(callback));
65-
}
61+
isConnectionSourcePinningRequired((isConnectionSourcePinningRequired, t) -> {
62+
if (t != null) {
63+
callback.onResult(null, t);
64+
} else if (isConnectionSourcePinningRequired) {
65+
getPinnedConnectionSource(true, callback);
66+
} else {
67+
wrapped.getReadConnectionSource(new WrappingCallback(callback));
68+
}
69+
});
6670
}
6771

6872
public void getWriteConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
69-
if (isConnectionSourcePinningRequired()) {
70-
getPinnedConnectionSource(false, callback);
71-
} else {
72-
wrapped.getWriteConnectionSource(new WrappingCallback(callback));
73-
}
73+
isConnectionSourcePinningRequired((isConnectionSourcePinningRequired, t) -> {
74+
if (t != null) {
75+
callback.onResult(null, t);
76+
} else if (isConnectionSourcePinningRequired) {
77+
getPinnedConnectionSource(false, callback);
78+
} else {
79+
wrapped.getWriteConnectionSource(new WrappingCallback(callback));
80+
}
81+
});
7482
}
7583

7684
@Override
@@ -138,6 +146,14 @@ public int release() {
138146
return count;
139147
}
140148

149+
private void isConnectionSourcePinningRequired(final SingleResultCallback<Boolean> callback) {
150+
try {
151+
callback.onResult(isConnectionSourcePinningRequired(), null);
152+
} catch (RuntimeException e) {
153+
callback.onResult(null, e);
154+
}
155+
}
156+
141157
private boolean isConnectionSourcePinningRequired() {
142158
ClusterType clusterType = wrapped.getCluster().getDescription().getType();
143159
return session.hasActiveTransaction() && (clusterType == ClusterType.SHARDED || clusterType == LOAD_BALANCED);

driver-core/src/main/com/mongodb/internal/connection/Cluster.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public interface Cluster extends Closeable {
5151
*
5252
* @return a ClusterDescription representing the current state of the cluster
5353
* @throws com.mongodb.MongoTimeoutException if the timeout has been reached before the cluster type is known
54+
* @throws com.mongodb.MongoInterruptedException if interrupted when getting the cluster description
5455
*/
5556
ClusterDescription getDescription();
5657

0 commit comments

Comments
 (0)