public CompletableFuture<RESP> sendRequest(final InetSocketAddress serverAddress, final REQ request) { if (clientShutdownFuture.get() != null) { return FutureUtils.getFailedFuture(new IllegalStateException(clientName + " is already shut down.")); } EstablishedConnection connection = establishedConnections.get(serverAddress); if (connection != null) { return connection.sendRequest(request); } else { PendingConnection pendingConnection = pendingConnections.get(serverAddress); if (pendingConnection != null) { // There was a race, use the existing pending connection. return pendingConnection.sendRequest(request); } else { // We try to connect to the server. PendingConnection pending = new PendingConnection(serverAddress, messageSerializer); PendingConnection previous = pendingConnections.putIfAbsent(serverAddress, pending); if (previous == null) { // OK, we are responsible to connect. bootstrap.connect(serverAddress.getAddress(), serverAddress.getPort()).addListener(pending); return pending.sendRequest(request); } else { // There was a race, use the existing pending connection. return previous.sendRequest(request); } } } }
public CompletableFuture<RESP> sendRequest(final InetSocketAddress serverAddress, final REQ request) { if (clientShutdownFuture.get() != null) { return FutureUtils.getFailedFuture(new IllegalStateException(clientName + " is already shut down.")); } EstablishedConnection connection = establishedConnections.get(serverAddress); if (connection != null) { return connection.sendRequest(request); } else { PendingConnection pendingConnection = pendingConnections.get(serverAddress); if (pendingConnection != null) { // There was a race, use the existing pending connection. return pendingConnection.sendRequest(request); } else { // We try to connect to the server. PendingConnection pending = new PendingConnection(serverAddress, messageSerializer); PendingConnection previous = pendingConnections.putIfAbsent(serverAddress, pending); if (previous == null) { // OK, we are responsible to connect. bootstrap.connect(serverAddress.getAddress(), serverAddress.getPort()).addListener(pending); return pending.sendRequest(request); } else { // There was a race, use the existing pending connection. return previous.sendRequest(request); } } } }
public CompletableFuture<RESP> sendRequest(final InetSocketAddress serverAddress, final REQ request) { if (clientShutdownFuture.get() != null) { return FutureUtils.getFailedFuture(new IllegalStateException(clientName + " is already shut down.")); } EstablishedConnection connection = establishedConnections.get(serverAddress); if (connection != null) { return connection.sendRequest(request); } else { PendingConnection pendingConnection = pendingConnections.get(serverAddress); if (pendingConnection != null) { // There was a race, use the existing pending connection. return pendingConnection.sendRequest(request); } else { // We try to connect to the server. PendingConnection pending = new PendingConnection(serverAddress, messageSerializer); PendingConnection previous = pendingConnections.putIfAbsent(serverAddress, pending); if (previous == null) { // OK, we are responsible to connect. bootstrap.connect(serverAddress.getAddress(), serverAddress.getPort()).addListener(pending); return pending.sendRequest(request); } else { // There was a race, use the existing pending connection. return previous.sendRequest(request); } } } }