diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxConnectivityChecker.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxConnectivityChecker.java index 8759aa595..8922948c6 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxConnectivityChecker.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxConnectivityChecker.java @@ -5,8 +5,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.mailbox.MailboxApi.ApiException; -import java.io.IOException; - import javax.annotation.concurrent.ThreadSafe; @ThreadSafe @@ -24,16 +22,11 @@ class ContactMailboxConnectivityChecker extends ConnectivityCheckerImpl { @Override ApiCall createConnectivityCheckTask(MailboxProperties properties) { if (properties.isOwner()) throw new IllegalArgumentException(); - return new SimpleApiCall() { - @Override - void tryToCallApi() throws IOException, ApiException { - if (!mailboxApi.checkStatus(properties)) { - throw new ApiException(); - } - // Call the observers and cache the result - onConnectivityCheckSucceeded(clock.currentTimeMillis()); - } - }; + return new SimpleApiCall(() -> { + if (!mailboxApi.checkStatus(properties)) throw new ApiException(); + // Call the observers and cache the result + onConnectivityCheckSucceeded(clock.currentTimeMillis()); + }); } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/SimpleApiCall.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/SimpleApiCall.java index 7c3e6f4b0..9a34a6244 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/SimpleApiCall.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/SimpleApiCall.java @@ -16,17 +16,20 @@ import static org.briarproject.bramble.util.LogUtils.logException; * Convenience class for making simple API calls that don't return values. */ @NotNullByDefault -public abstract class SimpleApiCall implements ApiCall { +class SimpleApiCall implements ApiCall { private static final Logger LOG = getLogger(SimpleApiCall.class.getName()); - abstract void tryToCallApi() - throws IOException, ApiException, TolerableFailureException; + private final Attempt attempt; + + SimpleApiCall(Attempt attempt) { + this.attempt = attempt; + } @Override public boolean callApi() { try { - tryToCallApi(); + attempt.tryToCallApi(); return false; // Succeeded, don't retry } catch (IOException | ApiException e) { logException(LOG, WARNING, e); @@ -36,4 +39,17 @@ public abstract class SimpleApiCall implements ApiCall { return false; // Failed tolerably, don't retry } } + + interface Attempt { + + /** + * Makes a single attempt to call an API endpoint. If this method + * throws an {@link IOException} or an {@link ApiException}, the call + * will be retried. If it throws a {@link TolerableFailureException} + * or returns without throwing an exception, the call will not be + * retried. + */ + void tryToCallApi() + throws IOException, ApiException, TolerableFailureException; + } }