Skip to content

Commit 34bfddb

Browse files
committed
Use async API in InternalStreamConnectionInitializer.startHandshake
1 parent c53b9ce commit 34bfddb

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636

3737
import java.util.List;
3838

39+
import static com.mongodb.assertions.Assertions.assertNotNull;
3940
import static com.mongodb.assertions.Assertions.notNull;
41+
import static com.mongodb.internal.async.AsyncRunnable.beginAsync;
4042
import static com.mongodb.internal.connection.CommandHelper.HELLO;
4143
import static com.mongodb.internal.connection.CommandHelper.LEGACY_HELLO;
4244
import static com.mongodb.internal.connection.CommandHelper.executeCommand;
@@ -73,9 +75,18 @@ public InternalStreamConnectionInitializer(final ClusterConnectionMode clusterCo
7375

7476
@Override
7577
public InternalConnectionInitializationDescription startHandshake(final InternalConnection internalConnection) {
76-
notNull("internalConnection", internalConnection);
78+
long startTime = System.nanoTime();
7779

78-
return initializeConnectionDescription(internalConnection);
80+
notNull("internalConnection", internalConnection);
81+
BsonDocument helloCommandDocument = createHelloCommand(authenticator, internalConnection);
82+
BsonDocument helloResult;
83+
try {
84+
helloResult = executeCommand("admin", helloCommandDocument, clusterConnectionMode, serverApi, internalConnection);
85+
} catch (MongoException e) {
86+
throw mapHelloException(e);
87+
}
88+
setSpeculativeAuthenticateResponse(helloResult);
89+
return createInitializationDescription(helloResult, internalConnection, startTime);
7990
}
8091

8192
public InternalConnectionInitializationDescription finishHandshake(final InternalConnection internalConnection,
@@ -91,15 +102,19 @@ public InternalConnectionInitializationDescription finishHandshake(final Interna
91102
public void startHandshakeAsync(final InternalConnection internalConnection,
92103
final SingleResultCallback<InternalConnectionInitializationDescription> callback) {
93104
long startTime = System.nanoTime();
94-
executeCommandAsync("admin", createHelloCommand(authenticator, internalConnection), clusterConnectionMode, serverApi,
95-
internalConnection, (helloResult, t) -> {
96-
if (t != null) {
97-
callback.onResult(null, t instanceof MongoException ? mapHelloException((MongoException) t) : t);
98-
} else {
99-
setSpeculativeAuthenticateResponse(helloResult);
100-
callback.onResult(createInitializationDescription(helloResult, internalConnection, startTime), null);
101-
}
102-
});
105+
beginAsync().<InternalConnectionInitializationDescription>thenSupply(c -> {
106+
notNull("internalConnection", internalConnection);
107+
BsonDocument helloCommandDocument = createHelloCommand(authenticator, internalConnection);
108+
109+
beginAsync().<BsonDocument>thenSupply(c2 -> {
110+
executeCommandAsync("admin", helloCommandDocument, clusterConnectionMode, serverApi, internalConnection, c2);
111+
}).onErrorIf(e -> e instanceof MongoException, (t, c2) -> {
112+
throw mapHelloException((MongoException) assertNotNull(t));
113+
}).thenApply((helloResult, c2) -> {
114+
setSpeculativeAuthenticateResponse(assertNotNull(helloResult));
115+
callback.complete(createInitializationDescription(helloResult, internalConnection, startTime));
116+
});
117+
}).finish(callback);
103118
}
104119

105120
@Override
@@ -121,20 +136,6 @@ public void finishHandshakeAsync(final InternalConnection internalConnection,
121136
}
122137
}
123138

124-
private InternalConnectionInitializationDescription initializeConnectionDescription(final InternalConnection internalConnection) {
125-
BsonDocument helloResult;
126-
BsonDocument helloCommandDocument = createHelloCommand(authenticator, internalConnection);
127-
128-
long start = System.nanoTime();
129-
try {
130-
helloResult = executeCommand("admin", helloCommandDocument, clusterConnectionMode, serverApi, internalConnection);
131-
} catch (MongoException e) {
132-
throw mapHelloException(e);
133-
}
134-
setSpeculativeAuthenticateResponse(helloResult);
135-
return createInitializationDescription(helloResult, internalConnection, start);
136-
}
137-
138139
private MongoException mapHelloException(final MongoException e) {
139140
if (checkSaslSupportedMechs && e.getCode() == USER_NOT_FOUND_CODE) {
140141
MongoCredential credential = authenticator.getMongoCredential();

0 commit comments

Comments
 (0)