Check if setException returns false and if so, cancel future.

Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
This commit is contained in:
HenrikJannsen 2023-01-04 18:09:04 -05:00
parent 5e29bfe4c2
commit 5efd13a678
No known key found for this signature in database
GPG Key ID: 02AA2BAE387C8307
4 changed files with 30 additions and 6 deletions

View File

@ -56,7 +56,10 @@ public class FeeRequest {
}
public void onFailure(@NotNull Throwable throwable) {
resultFuture.setException(throwable);
if (!resultFuture.setException(throwable)) {
// In case the setException returns false we need to cancel the future.
resultFuture.cancel(true);
}
}
}, MoreExecutors.directExecutor());

View File

@ -69,7 +69,10 @@ public class MempoolRequest {
}
public void onFailure(@NotNull Throwable throwable) {
mempoolServiceCallback.setException(throwable);
if (!mempoolServiceCallback.setException(throwable)) {
// In case the setException returns false we need to cancel the future.
mempoolServiceCallback.cancel(true);
}
}
}, MoreExecutors.directExecutor());
}

View File

@ -65,7 +65,10 @@ public class PriceRequest {
public void onFailure(@NotNull Throwable throwable) {
if (!shutDownRequested) {
resultFuture.setException(new PriceRequestException(throwable, baseUrl));
if (!resultFuture.setException(new PriceRequestException(throwable, baseUrl))) {
// In case the setException returns false we need to cancel the future.
resultFuture.cancel(true);
}
}
}
}, MoreExecutors.directExecutor());

View File

@ -240,7 +240,12 @@ public abstract class NetworkNode implements MessageListener {
public void onFailure(@NotNull Throwable throwable) {
log.debug("onFailure at sendMessage: peersNodeAddress={}\n\tmessage={}\n\tthrowable={}", peersNodeAddress, networkEnvelope.getClass().getSimpleName(), throwable.toString());
UserThread.execute(() -> resultFuture.setException(throwable));
UserThread.execute(() -> {
if (!resultFuture.setException(throwable)) {
// In case the setException returns false we need to cancel the future.
resultFuture.cancel(true);
}
});
}
}, MoreExecutors.directExecutor());
@ -311,13 +316,23 @@ public abstract class NetworkNode implements MessageListener {
}
public void onFailure(@NotNull Throwable throwable) {
UserThread.execute(() -> resultFuture.setException(throwable));
UserThread.execute(() -> {
if (!resultFuture.setException(throwable)) {
// In case the setException returns false we need to cancel the future.
resultFuture.cancel(true);
}
});
}
}, MoreExecutors.directExecutor());
} catch (RejectedExecutionException exception) {
log.error("RejectedExecutionException at sendMessage: ", exception);
resultFuture.setException(exception);
UserThread.execute(() -> {
if (!resultFuture.setException(exception)) {
// In case the setException returns false we need to cancel the future.
resultFuture.cancel(true);
}
});
}
return resultFuture;
}