Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/main/java/io/nats/client/impl/NatsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
Expand Down Expand Up @@ -119,6 +120,7 @@ class NatsConnection implements Connection {
private ExecutorService callbackRunner;

private ExecutorService executor;
private ExecutorService connectExecutor;

NatsConnection(Options options) {
this.options = options;
Expand Down Expand Up @@ -153,6 +155,7 @@ class NatsConnection implements Connection {
this.callbackRunner = Executors.newSingleThreadExecutor();

this.executor = options.getExecutor();
this.connectExecutor = Executors.newSingleThreadExecutor();
Copy link

@kevinfrommelt-wf kevinfrommelt-wf Apr 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// Connect is only called after creation
Expand Down Expand Up @@ -308,9 +311,21 @@ void tryToConnect(String serverURI) {

// Wait for the INFO message manually
// all other traffic will use the reader and writer
readInitialInfo();
checkVersionRequirements();
upgradeToSecureIfNeeded();
Callable<Object> connectTask = new Callable<Object>() {
public Object call() throws IOException {
readInitialInfo();
checkVersionRequirements();
upgradeToSecureIfNeeded();
return null;
}
};

Future<Object> future = this.connectExecutor.submit(connectTask);
try {
future.get(this.options.getConnectionTimeout().toNanos(), TimeUnit.NANOSECONDS);
} finally {
future.cancel(true);
}

// start the reader and writer after we secured the connection, if necessary
this.reader.start(this.dataPortFuture);
Expand Down Expand Up @@ -459,6 +474,7 @@ void closeSocket(boolean tryReconnectIfConnected) throws InterruptedException {
statusLock.lock();
try {
updateStatus(Status.DISCONNECTED);
this.exceptionDuringConnectChange = null; // Ignore IOExceptions during closeSocketImpl()
this.disconnecting = false;
statusChanged.signalAll();
} finally {
Expand Down Expand Up @@ -541,14 +557,17 @@ void close(boolean checkDrainStatus) throws InterruptedException {
statusLock.unlock();
}

// Stop the error handler code
// Stop the error handling and connect executors
callbackRunner.shutdown();
try {
callbackRunner.awaitTermination(this.options.getConnectionTimeout().toNanos(), TimeUnit.NANOSECONDS);
} finally {
callbackRunner.shutdownNow();
}

// There's no need to wait for running tasks since we're told to close
connectExecutor.shutdownNow();

statusLock.lock();
try {
this.disconnecting = false;
Expand Down