limit switch connection requests to 3 per minute

This commit is contained in:
woodser 2024-07-18 07:30:27 -04:00
parent 41e63805c1
commit cb132e727a

View file

@ -107,9 +107,9 @@ public final class XmrConnectionService {
// connection switching // connection switching
private static final int EXCLUDE_CONNECTION_SECONDS = 300; private static final int EXCLUDE_CONNECTION_SECONDS = 300;
private static final int SKIP_SWITCH_WITHIN_MS = 60000; private static final int MAX_SWITCH_REQUESTS_PER_MINUTE = 3;
private Set<MoneroRpcConnection> excludedConnections = new HashSet<>(); private Set<MoneroRpcConnection> excludedConnections = new HashSet<>();
private long lastSwitchRequestTimestamp; private int numRequestsLastMinute;
@Inject @Inject
public XmrConnectionService(P2PService p2PService, public XmrConnectionService(P2PService p2PService,
@ -279,24 +279,27 @@ public final class XmrConnectionService {
return false; return false;
} }
// skip if last switch was too recent // skip if too many requests in the last minute
boolean skipSwitch = System.currentTimeMillis() - lastSwitchRequestTimestamp < SKIP_SWITCH_WITHIN_MS; if (numRequestsLastMinute > MAX_SWITCH_REQUESTS_PER_MINUTE) {
lastSwitchRequestTimestamp = System.currentTimeMillis(); log.warn("Skipping switch to next best Monero connection because more than {} requests were made in the last minute", MAX_SWITCH_REQUESTS_PER_MINUTE);
if (skipSwitch) {
log.warn("Skipping switch to next best Monero connection because last switch was less than {} seconds ago", SKIP_SWITCH_WITHIN_MS / 1000);
lastSwitchRequestTimestamp = System.currentTimeMillis();
return false; return false;
} }
// try to get connection to switch to // increment request count
numRequestsLastMinute++;
UserThread.runAfter(() -> numRequestsLastMinute--, 60); // decrement after one minute
// exclude current connection
MoneroRpcConnection currentConnection = getConnection(); MoneroRpcConnection currentConnection = getConnection();
if (currentConnection != null) excludedConnections.add(currentConnection); if (currentConnection != null) excludedConnections.add(currentConnection);
// get connection to switch to
MoneroRpcConnection bestConnection = getBestAvailableConnection(excludedConnections); MoneroRpcConnection bestConnection = getBestAvailableConnection(excludedConnections);
// remove from excluded connections after period // remove from excluded connections after period
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
if (currentConnection != null) excludedConnections.remove(currentConnection); if (currentConnection != null) excludedConnections.remove(currentConnection);
}, EXCLUDE_CONNECTION_SECONDS); }, EXCLUDE_CONNECTION_SECONDS);
// switch to best connection // switch to best connection
if (bestConnection == null) { if (bestConnection == null) {