Add MailboxApiCaller for calling API endpoints with retries.

This commit is contained in:
akwizgran
2022-05-23 17:43:52 +01:00
parent 18b3865a86
commit 274963d9d1
4 changed files with 298 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package org.briarproject.bramble.mailbox;
import org.briarproject.bramble.api.Cancellable;
import org.briarproject.bramble.api.Supplier;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.mailbox.MailboxApi.TolerableFailureException;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MINUTES;
@NotNullByDefault
interface MailboxApiCaller {
/**
* The minimum interval between retries in milliseconds.
*/
long MIN_RETRY_INTERVAL_MS = MINUTES.toMillis(1);
/**
* The maximum interval between retries in milliseconds.
*/
long MAX_RETRY_INTERVAL_MS = DAYS.toMillis(1);
/**
* Asynchronously calls the given supplier, automatically retrying at
* increasing intervals until the supplier returns false. The returned
* {@link Cancellable} can be used to cancel any future retries.
*
* @param supplier A wrapper for an API call. The supplier's
* {@link Supplier#get() get()} method will be called on the
* {@link IoExecutor}. It should return true if the API call needs to be
* retried, or false if the API call succeeded or
* {@link TolerableFailureException failed tolerably}.
*/
Cancellable retryWithBackoff(Supplier<Boolean> supplier);
}

View File

@@ -0,0 +1,100 @@
package org.briarproject.bramble.mailbox;
import org.briarproject.bramble.api.Cancellable;
import org.briarproject.bramble.api.Supplier;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import static java.lang.Math.min;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@Immutable
@NotNullByDefault
class MailboxApiCallerImpl implements MailboxApiCaller {
private final TaskScheduler taskScheduler;
private final Executor ioExecutor;
@Inject
MailboxApiCallerImpl(TaskScheduler taskScheduler,
@IoExecutor Executor ioExecutor) {
this.taskScheduler = taskScheduler;
this.ioExecutor = ioExecutor;
}
@Override
public Cancellable retryWithBackoff(Supplier<Boolean> supplier) {
Task task = new Task(supplier);
task.start();
return task;
}
private class Task implements Cancellable {
private final Supplier<Boolean> supplier;
private final Object lock = new Object();
@GuardedBy("lock")
@Nullable
private Cancellable scheduledTask = null;
@GuardedBy("lock")
private boolean cancelled = false;
@GuardedBy("lock")
private long retryIntervalMs = MIN_RETRY_INTERVAL_MS;
private Task(Supplier<Boolean> supplier) {
this.supplier = supplier;
}
private void start() {
synchronized (lock) {
if (cancelled) throw new AssertionError();
scheduledTask = taskScheduler.schedule(this::callApi,
ioExecutor, 0, MILLISECONDS);
}
}
@IoExecutor
private void callApi() {
synchronized (lock) {
if (cancelled) return;
}
// The supplier returns true if we should retry
if (supplier.get()) {
synchronized (lock) {
if (cancelled) return;
scheduledTask = taskScheduler.schedule(this::callApi,
ioExecutor, retryIntervalMs, MILLISECONDS);
// Increase the retry interval each time we retry
retryIntervalMs =
min(MAX_RETRY_INTERVAL_MS, retryIntervalMs * 2);
}
} else {
synchronized (lock) {
scheduledTask = null;
}
}
}
@Override
public void cancel() {
Cancellable scheduledTask;
synchronized (lock) {
cancelled = true;
scheduledTask = this.scheduledTask;
this.scheduledTask = null;
}
if (scheduledTask != null) scheduledTask.cancel();
}
}
}