poll daemon and trade wallets in dedicated lock and prevent queuing

This commit is contained in:
woodser 2023-12-20 07:12:49 -05:00
parent e1ca35172d
commit a1f8f942fc
2 changed files with 29 additions and 17 deletions

View file

@ -52,6 +52,7 @@ public final class XmrConnectionService {
private static Long lastErrorTimestamp;
private final Object lock = new Object();
private final Object pollLock = new Object();
private final Object listenerLock = new Object();
private final Config config;
private final CoreContext coreContext;
@ -70,6 +71,7 @@ public final class XmrConnectionService {
private Socks5ProxyProvider socks5ProxyProvider;
private boolean isInitialized;
private boolean pollInProgress;
private MoneroDaemonRpc daemon;
@Getter
private MoneroDaemonInfo lastInfo;
@ -575,7 +577,9 @@ public final class XmrConnectionService {
}
private void pollDaemonInfo() {
synchronized (lock) {
if (pollInProgress) return;
synchronized (pollLock) {
pollInProgress = true;
if (isShutDownStarted) return;
try {
@ -651,6 +655,8 @@ public final class XmrConnectionService {
if (!Boolean.TRUE.equals(connectionManager.isConnected()) && HavenoUtils.havenoSetup != null) {
HavenoUtils.havenoSetup.getWalletServiceErrorMsg().set(e.getMessage());
}
} finally {
pollInProgress = false;
}
}
}

View file

@ -116,9 +116,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
public abstract class Trade implements Tradable, Model {
private static final String MONERO_TRADE_WALLET_PREFIX = "xmr_trade_";
private MoneroWallet wallet; // trade wallet
private Object walletLock = new Object();
boolean wasWalletSynced = false;
private final Object walletLock = new Object();
private final Object pollLock = new Object();
private MoneroWallet wallet;
boolean wasWalletSynced;
boolean pollInProgress;
///////////////////////////////////////////////////////////////////////////////////////////
// Enums
@ -1911,7 +1913,7 @@ public abstract class Trade implements Tradable, Model {
log.info("Setting wallet refresh rate for {} {} to {}", getClass().getSimpleName(), getId(), getWalletRefreshPeriod());
getWallet().startSyncing(getWalletRefreshPeriod()); // TODO (monero-project): wallet rpc waits until last sync period finishes before starting new sync period
}
if (isPolling()) {
if (isPollInProgress()) {
stopPolling();
startPolling();
}
@ -1920,7 +1922,7 @@ public abstract class Trade implements Tradable, Model {
private void startPolling() {
synchronized (walletLock) {
if (isShutDownStarted || isPolling()) return;
if (isShutDownStarted || isPollInProgress()) return;
log.info("Starting to poll wallet for {} {}", getClass().getSimpleName(), getId());
txPollLooper = new TaskLooper(() -> pollWallet());
txPollLooper.start(walletRefreshPeriodMs);
@ -1929,22 +1931,24 @@ public abstract class Trade implements Tradable, Model {
private void stopPolling() {
synchronized (walletLock) {
if (isPolling()) {
if (isPollInProgress()) {
txPollLooper.stop();
txPollLooper = null;
}
}
}
private boolean isPolling() {
private boolean isPollInProgress() {
synchronized (walletLock) {
return txPollLooper != null;
}
}
private void pollWallet() {
try {
synchronized (walletLock) {
if (pollInProgress) return;
synchronized (pollLock) {
pollInProgress = true;
try {
// log warning if wallet is too far behind daemon
MoneroDaemonInfo lastInfo = xmrConnectionService.getLastInfo();
@ -2026,13 +2030,15 @@ public abstract class Trade implements Tradable, Model {
}
}
}
}
} catch (Exception e) {
boolean isWalletConnected = isWalletConnectedToDaemon();
if (!isWalletConnected) xmrConnectionService.checkConnection(); // check connection if wallet is not connected
if (!isShutDownStarted && wallet != null && isWalletConnected) {
e.printStackTrace();
log.warn("Error polling trade wallet for {} {}: {}. Monerod={}", getClass().getSimpleName(), getId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection());
} catch (Exception e) {
boolean isWalletConnected = isWalletConnectedToDaemon();
if (!isWalletConnected) xmrConnectionService.checkConnection(); // check connection if wallet is not connected
if (!isShutDownStarted && wallet != null && isWalletConnected) {
e.printStackTrace();
log.warn("Error polling trade wallet for {} {}: {}. Monerod={}", getClass().getSimpleName(), getId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection());
}
} finally {
pollInProgress = false;
}
}
}