Skip to content

Commit 4f5f3a5

Browse files
committed
connection service switches connection after max attempts
1 parent 7128664 commit 4f5f3a5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

core/src/main/java/haveno/core/api/XmrConnectionService.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public final class XmrConnectionService {
7474
private static final long REFRESH_PERIOD_ONION_MS = 30000; // refresh period when connected to remote node over tor
7575
private static final long KEY_IMAGE_REFRESH_PERIOD_MS_LOCAL = 20000; // 20 seconds
7676
private static final long KEY_IMAGE_REFRESH_PERIOD_MS_REMOTE = 300000; // 5 minutes
77+
private static final int MAX_CONSECUTIVE_ERRORS = 4; // max errors before switching connections
78+
private static int numConsecutiveErrors = 0;
7779

7880
public enum XmrConnectionFallbackType {
7981
LOCAL,
@@ -786,11 +788,20 @@ private void doPollMonerod() {
786788
try {
787789
if (monerod == null) throw new RuntimeException("No connection to Monero daemon");
788790
lastInfo = monerod.getInfo();
791+
numConsecutiveErrors = 0;
789792
} catch (Exception e) {
790793

791794
// skip handling if shutting down
792795
if (isShutDownStarted) return;
793796

797+
// skip error handling up to max attempts
798+
numConsecutiveErrors++;
799+
if (numConsecutiveErrors <= MAX_CONSECUTIVE_ERRORS) {
800+
return;
801+
} else {
802+
numConsecutiveErrors = 0; // reset error count
803+
}
804+
794805
// invoke fallback handling on startup error
795806
boolean canFallback = isFixedConnection() || isProvidedConnections() || isCustomConnections() || usedSyncingLocalNodeBeforeStartup;
796807
if (lastInfo == null && canFallback) {
@@ -811,8 +822,9 @@ private void doPollMonerod() {
811822
}
812823

813824
// log error message periodically
814-
if (lastLogPollErrorTimestamp == null || System.currentTimeMillis() - lastLogPollErrorTimestamp > HavenoUtils.LOG_POLL_ERROR_PERIOD_MS) {
815-
log.warn("Failed to fetch monerod info, trying to switch to best connection, error={}", e.getMessage());
825+
if (lastWarningOutsidePeriod()) {
826+
MoneroRpcConnection connection = getConnection();
827+
log.warn("Error fetching daemon info after max attempts. Trying to switch to best connection. monerod={}, error={}", connection == null ? "null" : connection.getUri(), e.getMessage());
816828
if (DevEnv.isDevMode()) log.error(ExceptionUtils.getStackTrace(e));
817829
lastLogPollErrorTimestamp = System.currentTimeMillis();
818830
}
@@ -911,6 +923,10 @@ else if (lastInfo.isBusySyncing()) {
911923
}
912924
}
913925

926+
private boolean lastWarningOutsidePeriod() {
927+
return lastLogPollErrorTimestamp == null || System.currentTimeMillis() - lastLogPollErrorTimestamp > HavenoUtils.LOG_POLL_ERROR_PERIOD_MS;
928+
}
929+
914930
private boolean isFixedConnection() {
915931
return !"".equals(config.xmrNode) && !(HavenoUtils.isLocalHost(config.xmrNode) && xmrLocalNode.shouldBeIgnored()) && !fallbackApplied;
916932
}

0 commit comments

Comments
 (0)