Refactor SimpleApiCall to support lambdas.

This commit is contained in:
akwizgran
2022-05-27 16:33:55 +01:00
parent bb71de1a78
commit 2b4a1cf54b
2 changed files with 25 additions and 16 deletions

View File

@@ -5,8 +5,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.mailbox.MailboxApi.ApiException; import org.briarproject.bramble.mailbox.MailboxApi.ApiException;
import java.io.IOException;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe @ThreadSafe
@@ -24,16 +22,11 @@ class ContactMailboxConnectivityChecker extends ConnectivityCheckerImpl {
@Override @Override
ApiCall createConnectivityCheckTask(MailboxProperties properties) { ApiCall createConnectivityCheckTask(MailboxProperties properties) {
if (properties.isOwner()) throw new IllegalArgumentException(); if (properties.isOwner()) throw new IllegalArgumentException();
return new SimpleApiCall() { return new SimpleApiCall(() -> {
@Override if (!mailboxApi.checkStatus(properties)) throw new ApiException();
void tryToCallApi() throws IOException, ApiException { // Call the observers and cache the result
if (!mailboxApi.checkStatus(properties)) { onConnectivityCheckSucceeded(clock.currentTimeMillis());
throw new ApiException(); });
}
// Call the observers and cache the result
onConnectivityCheckSucceeded(clock.currentTimeMillis());
}
};
} }
} }

View File

@@ -16,17 +16,20 @@ import static org.briarproject.bramble.util.LogUtils.logException;
* Convenience class for making simple API calls that don't return values. * Convenience class for making simple API calls that don't return values.
*/ */
@NotNullByDefault @NotNullByDefault
public abstract class SimpleApiCall implements ApiCall { class SimpleApiCall implements ApiCall {
private static final Logger LOG = getLogger(SimpleApiCall.class.getName()); private static final Logger LOG = getLogger(SimpleApiCall.class.getName());
abstract void tryToCallApi() private final Attempt attempt;
throws IOException, ApiException, TolerableFailureException;
SimpleApiCall(Attempt attempt) {
this.attempt = attempt;
}
@Override @Override
public boolean callApi() { public boolean callApi() {
try { try {
tryToCallApi(); attempt.tryToCallApi();
return false; // Succeeded, don't retry return false; // Succeeded, don't retry
} catch (IOException | ApiException e) { } catch (IOException | ApiException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
@@ -36,4 +39,17 @@ public abstract class SimpleApiCall implements ApiCall {
return false; // Failed tolerably, don't retry 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;
}
} }