Factor mailbox problem detection into MailboxStatus and constants

This commit is contained in:
Torsten Grote
2022-05-25 13:06:54 -03:00
parent 6c19b22aab
commit 54339afab8
4 changed files with 27 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
package org.briarproject.bramble.api.mailbox;
import static java.util.concurrent.TimeUnit.HOURS;
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.bramble.api.transport.TransportConstants.STREAM_HEADER_LENGTH;
@@ -20,4 +21,17 @@ public interface MailboxConstants {
int MAX_FILE_PAYLOAD_BYTES =
(MAX_FILE_BYTES - TAG_LENGTH - STREAM_HEADER_LENGTH)
/ MAX_FRAME_LENGTH * MAX_PAYLOAD_LENGTH;
/**
* The number of connection failures
* that indicate a problem with the mailbox.
*/
int PROBLEM_NUM_CONNECTION_FAILURES = 5;
/**
* The time in milliseconds since the last connection success
* that need to pass to indicates a problem with the mailbox.
*/
long PROBLEM_MS_SINCE_LAST_SUCCESS = HOURS.toMillis(1);
}

View File

@@ -4,6 +4,9 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.concurrent.Immutable;
import static org.briarproject.bramble.api.mailbox.MailboxConstants.PROBLEM_MS_SINCE_LAST_SUCCESS;
import static org.briarproject.bramble.api.mailbox.MailboxConstants.PROBLEM_NUM_CONNECTION_FAILURES;
@Immutable
@NotNullByDefault
public class MailboxStatus {
@@ -56,4 +59,12 @@ public class MailboxStatus {
public int getAttemptsSinceSuccess() {
return attemptsSinceSuccess;
}
/**
* @return true if this status indicates a problem with the mailbox.
*/
public boolean hasProblem(long now) {
return attemptsSinceSuccess >= PROBLEM_NUM_CONNECTION_FAILURES &&
(now - lastSuccess) >= PROBLEM_MS_SINCE_LAST_SUCCESS;
}
}

View File

@@ -23,7 +23,6 @@ import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import static java.util.concurrent.TimeUnit.DAYS;
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
@Immutable
@@ -143,9 +142,7 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
settingsManager.mergeSettings(txn, newSettings, SETTINGS_NAMESPACE);
MailboxStatus status = new MailboxStatus(now, lastSuccess, newAttempts);
txn.attach(new OwnMailboxConnectionStatusEvent(status));
if (now - lastSuccess > DAYS.toMillis(3) && newAttempts > 10) {
txn.attach(new MailboxProblemEvent());
}
if (status.hasProblem(now)) txn.attach(new MailboxProblemEvent());
}
@Override

View File

@@ -139,7 +139,7 @@ public class MailboxStatusFragment extends Fragment {
tintRes = R.color.briar_brand_green;
showUnlinkWarning = true;
wizardButton.setVisibility(GONE);
} else if (status.getAttemptsSinceSuccess() < NUM_FAILURES) {
} else if (!status.hasProblem(System.currentTimeMillis())) {
iconRes = R.drawable.ic_help_outline_white;
title = getString(R.string.mailbox_status_problem_title);
tintRes = R.color.briar_orange_500;