Factor out mailbox constants into a MailboxConfig

so that we can change them for integration tests via the new ModularMailboxModule that now also includes the UrlProvider
This commit is contained in:
Torsten Grote
2022-10-21 11:35:25 -03:00
parent 28f770df89
commit bab6ec70f5
27 changed files with 152 additions and 51 deletions

View File

@@ -20,12 +20,15 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
class MailboxApiCallerImpl implements MailboxApiCaller {
private final TaskScheduler taskScheduler;
private final MailboxConfig mailboxConfig;
private final Executor ioExecutor;
@Inject
MailboxApiCallerImpl(TaskScheduler taskScheduler,
MailboxConfig mailboxConfig,
@IoExecutor Executor ioExecutor) {
this.taskScheduler = taskScheduler;
this.mailboxConfig = mailboxConfig;
this.ioExecutor = ioExecutor;
}
@@ -49,7 +52,8 @@ class MailboxApiCallerImpl implements MailboxApiCaller {
private boolean cancelled = false;
@GuardedBy("lock")
private long retryIntervalMs = MIN_RETRY_INTERVAL_MS;
private long retryIntervalMs =
mailboxConfig.getApiCallerMinRetryInterval();
private Task(ApiCall apiCall) {
this.apiCall = apiCall;
@@ -74,8 +78,9 @@ class MailboxApiCallerImpl implements MailboxApiCaller {
scheduledTask = taskScheduler.schedule(this::callApi,
ioExecutor, retryIntervalMs, MILLISECONDS);
// Increase the retry interval each time we retry
retryIntervalMs =
min(MAX_RETRY_INTERVAL_MS, retryIntervalMs * 2);
retryIntervalMs = min(
mailboxConfig.getApiCallerMaxRetryInterval(),
retryIntervalMs * 2);
}
} else {
synchronized (lock) {

View File

@@ -0,0 +1,24 @@
package org.briarproject.bramble.mailbox;
import org.briarproject.bramble.api.plugin.Plugin;
interface MailboxConfig {
/**
* The minimum interval between API call retries in milliseconds.
*/
long getApiCallerMinRetryInterval();
/**
* The maximum interval between API call retries in milliseconds.
*/
long getApiCallerMaxRetryInterval();
/**
* How long (in milliseconds) the Tor plugin needs to be continuously
* {@link Plugin.State#ACTIVE active} before we assume our contacts can
* reach our hidden service.
*/
long getTorReachabilityPeriod();
}

View File

@@ -0,0 +1,30 @@
package org.briarproject.bramble.mailbox;
import org.briarproject.nullsafety.NotNullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
@Immutable
@NotNullByDefault
class MailboxConfigImpl implements MailboxConfig {
@Inject
MailboxConfigImpl() {
}
@Override
public long getApiCallerMinRetryInterval() {
return MailboxApiCaller.MIN_RETRY_INTERVAL_MS;
}
@Override
public long getApiCallerMaxRetryInterval() {
return MailboxApiCaller.MAX_RETRY_INTERVAL_MS;
}
@Override
public long getTorReachabilityPeriod() {
return TorReachabilityMonitor.REACHABILITY_PERIOD_MS;
}
}

View File

@@ -4,7 +4,11 @@ import dagger.Module;
import dagger.Provides;
@Module
public class UrlConverterModule {
public class ModularMailboxModule {
@Provides
MailboxConfig provideMailboxConfig(MailboxConfigImpl mailboxConfig) {
return mailboxConfig;
}
@Provides
UrlConverter provideUrlConverter(UrlConverterImpl urlConverter) {

View File

@@ -32,6 +32,7 @@ class TorReachabilityMonitorImpl
private final Executor ioExecutor;
private final TaskScheduler taskScheduler;
private final MailboxConfig mailboxConfig;
private final PluginManager pluginManager;
private final EventBus eventBus;
private final Object lock = new Object();
@@ -50,10 +51,12 @@ class TorReachabilityMonitorImpl
TorReachabilityMonitorImpl(
@IoExecutor Executor ioExecutor,
TaskScheduler taskScheduler,
MailboxConfig mailboxConfig,
PluginManager pluginManager,
EventBus eventBus) {
this.ioExecutor = ioExecutor;
this.taskScheduler = taskScheduler;
this.mailboxConfig = mailboxConfig;
this.pluginManager = pluginManager;
this.eventBus = eventBus;
}
@@ -110,7 +113,7 @@ class TorReachabilityMonitorImpl
synchronized (lock) {
if (destroyed || task != null) return;
task = taskScheduler.schedule(this::onTorReachable, ioExecutor,
REACHABILITY_PERIOD_MS, MILLISECONDS);
mailboxConfig.getTorReachabilityPeriod(), MILLISECONDS);
}
}