Skip to content

Commit

Permalink
Compute Tor bootstrap timeout from last event
Browse files Browse the repository at this point in the history
When the Tor network is under load, the bootstrap process takes multiple
minutes. Tor informs us about the progress with BootstrapEvent's until
Tor is ready.

Fixes bisq-network#1798
  • Loading branch information
alvasw committed Apr 15, 2024
1 parent a6980b3 commit 5226c34
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import java.io.IOException;
import java.net.Socket;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -139,11 +141,14 @@ public boolean isHiddenServiceAvailable(String onionUrl) {

public void waitUntilBootstrapped() {
try {
boolean isSuccess = isBootstrappedCountdownLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);
if (isSuccess) {
removeBootstrapEventListener();
} else {
throw new TorBootstrapFailedException("Tor bootstrap timeout triggered.");
while (true) {
boolean isSuccess = isBootstrappedCountdownLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);
if (isSuccess) {
removeBootstrapEventListener();
break;
} else if (isBootstrapTimeoutTriggered()) {
throw new TorBootstrapFailedException("Tor bootstrap timeout triggered.");
}
}
} catch (InterruptedException e) {
throw new TorBootstrapFailedException(e);
Expand Down Expand Up @@ -221,4 +226,11 @@ private void removeHsDescUploadedEventListener() {
controllerEventHandler.removeHsDescUploadedListener(this);
clearAllEventSubscriptionsOnConnection(controlConnection);
}

private boolean isBootstrapTimeoutTriggered() {
BootstrapEvent bootstrapEvent = this.bootstrapEvent.get();
Instant timestamp = bootstrapEvent.getTimestamp();
Instant bootstrapTimeoutAgo = Instant.now().minus(bootstrapTimeout, ChronoUnit.MILLIS);
return bootstrapTimeoutAgo.isAfter(timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import lombok.Getter;
import lombok.ToString;

import java.time.Instant;

@Builder
@Getter
@ToString
Expand All @@ -32,6 +34,8 @@ public class BootstrapEvent {
private final String tag;
private final String summary;

private final Instant timestamp = Instant.now();

public BootstrapEvent(int progress, String tag, String summary) {
if (progress < 0 || tag.isEmpty() || summary.isEmpty()) {
throw new IllegalArgumentException("Invalid bootstrap event: " + progress + " " + tag + " " + summary);
Expand Down

0 comments on commit 5226c34

Please sign in to comment.