From fc3407cd50f4c90e3fbbce6ab8ac9f13d3da3972 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 20 Jul 2024 14:06:40 -0400 Subject: [PATCH] ignore switch connection request within 10s, 2 per min --- .../haveno/core/api/XmrConnectionService.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index d44d366212..caddf2999e 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -106,10 +106,12 @@ public final class XmrConnectionService { private List listeners = new ArrayList<>(); // connection switching - private static final int EXCLUDE_CONNECTION_SECONDS = 300; - private static final int MAX_SWITCH_REQUESTS_PER_MINUTE = 3; - private Set excludedConnections = new HashSet<>(); + private static final int EXCLUDE_CONNECTION_SECONDS = 180; + private static final int MAX_SWITCH_REQUESTS_PER_MINUTE = 2; + private static final int SKIP_SWITCH_WITHIN_MS = 10000; private int numRequestsLastMinute; + private long lastSwitchTimestamp; + private Set excludedConnections = new HashSet<>(); @Inject public XmrConnectionService(P2PService p2PService, @@ -270,15 +272,28 @@ public final class XmrConnectionService { if (bestConnection != null) setConnection(bestConnection); } - public boolean requestSwitchToNextBestConnection() { + public synchronized boolean requestSwitchToNextBestConnection() { log.warn("Request made to switch to next best monerod, current monerod={}", getConnection() == null ? null : getConnection().getUri()); + // skip if shut down started + if (isShutDownStarted) { + log.warn("Skipping switch to next best Monero connection because shut down has started"); + return false; + } + // skip if connection is fixed if (isFixedConnection() || !connectionManager.getAutoSwitch()) { log.info("Skipping switch to next best Monero connection because connection is fixed or auto switch is disabled"); return false; } + // skip if last switch was too recent + boolean skipSwitch = System.currentTimeMillis() - lastSwitchTimestamp < SKIP_SWITCH_WITHIN_MS; + if (skipSwitch) { + log.warn("Skipping switch to next best Monero connection because last switch was less than {} seconds ago", SKIP_SWITCH_WITHIN_MS / 1000); + return false; + } + // skip if too many requests in the last minute if (numRequestsLastMinute > MAX_SWITCH_REQUESTS_PER_MINUTE) { log.warn("Skipping switch to next best Monero connection because more than {} requests were made in the last minute", MAX_SWITCH_REQUESTS_PER_MINUTE); @@ -301,11 +316,14 @@ public final class XmrConnectionService { if (currentConnection != null) excludedConnections.remove(currentConnection); }, EXCLUDE_CONNECTION_SECONDS); - // switch to best connection + // return if no connection to switch to if (bestConnection == null) { log.warn("No connection to switch to"); return false; } + + // switch to best connection + lastSwitchTimestamp = System.currentTimeMillis(); setConnection(bestConnection); return true; }