Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tor: Move RetryPolicy to start-up thread #858

Merged
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
34 changes: 17 additions & 17 deletions network/tor/src/main/java/bisq/tor/Tor.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public boolean isStartingOrRunning() {
private final TorBootstrap torBootstrap;
private final String torDirPath;
private final AtomicReference<State> state = new AtomicReference<>(State.NEW);
private final RetryPolicy<Boolean> retryPolicy;
private int proxyPort = -1;

public static Tor getTor(String torDirPath) {
Expand All @@ -105,26 +104,27 @@ private Tor(String torDirPath) {
Thread.currentThread().setName("Tor.shutdownHook");
shutdown();
}));

retryPolicy = RetryPolicy.<Boolean>builder()
.handle(IllegalStateException.class)
.handleResultIf(result -> state.get() == STARTING)
.withBackoff(Duration.ofSeconds(3), Duration.ofSeconds(30))
.withJitter(0.25)
.withMaxDuration(Duration.ofMinutes(5)).withMaxRetries(30)
.onRetry(e -> log.info("Retry. AttemptCount={}.", e.getAttemptCount()))
.onRetriesExceeded(e -> {
log.warn("Failed. Max retries exceeded. We shutdown.");
shutdown();
})
.onSuccess(e -> log.debug("Succeeded."))
.build();
}

public CompletableFuture<Boolean> startAsync(ExecutorService executor) {
return CompletableFuture.supplyAsync(
() -> Failsafe.with(retryPolicy)
.get(Tor.this::doStart),
() -> {
var retryPolicy = RetryPolicy.<Boolean>builder()
.handle(IllegalStateException.class)
.handleResultIf(result -> state.get() == STARTING)
.withBackoff(Duration.ofSeconds(3), Duration.ofSeconds(30))
.withJitter(0.25)
.withMaxDuration(Duration.ofMinutes(5)).withMaxRetries(30)
.onRetry(e -> log.info("Retry. AttemptCount={}.", e.getAttemptCount()))
.onRetriesExceeded(e -> {
log.warn("Failed. Max retries exceeded. We shutdown.");
shutdown();
})
.onSuccess(e -> log.debug("Succeeded."))
.build();

return Failsafe.with(retryPolicy).get(Tor.this::doStart);
},
executor
);
}
Expand Down