Skip to content

Commit

Permalink
ignore network node errors if shutting down
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jul 6, 2024
1 parent 8ce91aa commit bf9d5c7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,38 @@ void requestData(NodeAddress nodeAddress, boolean isPreliminaryDataRequest) {
getDataRequestType = getDataRequest.getClass().getSimpleName();
log.info("\n\n>> We send a {} to peer {}\n", getDataRequestType, nodeAddress);
networkNode.addMessageListener(this);
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest);
//noinspection UnstableApiUsage
Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
log.trace("Send {} to {} succeeded.", getDataRequest, nodeAddress);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call." +
"Might be caused by a previous timeout.");
}
}

@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending getDataRequest to " + nodeAddress +
" failed. That is expected if the peer is offline.\n\t" +
"getDataRequest=" + getDataRequest + "." +
"\n\tException=" + throwable.getMessage();
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call. " +
"Might be caused by a previous timeout.");
try {
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest);
//noinspection UnstableApiUsage
Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
log.trace("Send {} to {} succeeded.", getDataRequest, nodeAddress);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call." +
"Might be caused by a previous timeout.");
}
}
}
}, MoreExecutors.directExecutor());

@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending getDataRequest to " + nodeAddress +
" failed. That is expected if the peer is offline.\n\t" +
"getDataRequest=" + getDataRequest + "." +
"\n\tException=" + throwable.getMessage();
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call. " +
"Might be caused by a previous timeout.");
}
}
}, MoreExecutors.directExecutor());
} catch (Exception e) {
if (!networkNode.isShutDownStarted()) throw e;
}
} else {
log.warn("We have stopped already. We ignore that requestData call.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,39 @@ private void sendGetPeersRequest(NodeAddress nodeAddress) {
TIMEOUT, TimeUnit.SECONDS);
}

SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getPeersRequest);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
//TODO
/*if (!connection.getPeersNodeAddressOptional().isPresent()) {
connection.setPeersNodeAddress(nodeAddress);
log.warn("sendGetPeersRequest: !connection.getPeersNodeAddressOptional().isPresent()");
}*/

PeerExchangeHandler.this.connection = connection;
connection.addMessageListener(PeerExchangeHandler.this);
} else {
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onSuccess call.");
try {
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getPeersRequest);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
//TODO
/*if (!connection.getPeersNodeAddressOptional().isPresent()) {
connection.setPeersNodeAddress(nodeAddress);
log.warn("sendGetPeersRequest: !connection.getPeersNodeAddressOptional().isPresent()");
}*/

PeerExchangeHandler.this.connection = connection;
connection.addMessageListener(PeerExchangeHandler.this);
} else {
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onSuccess call.");
}
}
}

@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending getPeersRequest to " + nodeAddress +
" failed. That is expected if the peer is offline. Exception=" + throwable.getMessage();
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, nodeAddress);
} else {
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onFailure call.");

@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending getPeersRequest to " + nodeAddress +
" failed. That is expected if the peer is offline. Exception=" + throwable.getMessage();
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, nodeAddress);
} else {
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onFailure call.");
}
}
}
}, MoreExecutors.directExecutor());
}, MoreExecutors.directExecutor());
} catch (Exception e) {
if (!networkNode.isShutDownStarted()) throw e;
}
} else {
log.debug("My node address is still null at sendGetPeersRequest. We ignore that call.");
}
Expand Down

0 comments on commit bf9d5c7

Please sign in to comment.