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 static Long lastErrorTimestamp;
private final Object lock = new Object(); private final Object lock = new Object();
private final Object pollLock = new Object();
private final Object listenerLock = new Object(); private final Object listenerLock = new Object();
private final Config config; private final Config config;
private final CoreContext coreContext; private final CoreContext coreContext;
@ -70,6 +71,7 @@ public final class XmrConnectionService {
private Socks5ProxyProvider socks5ProxyProvider; private Socks5ProxyProvider socks5ProxyProvider;
private boolean isInitialized; private boolean isInitialized;
private boolean pollInProgress;
private MoneroDaemonRpc daemon; private MoneroDaemonRpc daemon;
@Getter @Getter
private MoneroDaemonInfo lastInfo; private MoneroDaemonInfo lastInfo;
@ -575,7 +577,9 @@ public final class XmrConnectionService {
} }
private void pollDaemonInfo() { private void pollDaemonInfo() {
synchronized (lock) { if (pollInProgress) return;
synchronized (pollLock) {
pollInProgress = true;
if (isShutDownStarted) return; if (isShutDownStarted) return;
try { try {
@ -651,6 +655,8 @@ public final class XmrConnectionService {
if (!Boolean.TRUE.equals(connectionManager.isConnected()) && HavenoUtils.havenoSetup != null) { if (!Boolean.TRUE.equals(connectionManager.isConnected()) && HavenoUtils.havenoSetup != null) {
HavenoUtils.havenoSetup.getWalletServiceErrorMsg().set(e.getMessage()); 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 { public abstract class Trade implements Tradable, Model {
private static final String MONERO_TRADE_WALLET_PREFIX = "xmr_trade_"; private static final String MONERO_TRADE_WALLET_PREFIX = "xmr_trade_";
private MoneroWallet wallet; // trade wallet private final Object walletLock = new Object();
private Object walletLock = new Object(); private final Object pollLock = new Object();
boolean wasWalletSynced = false; private MoneroWallet wallet;
boolean wasWalletSynced;
boolean pollInProgress;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Enums // Enums
@ -1911,7 +1913,7 @@ public abstract class Trade implements Tradable, Model {
log.info("Setting wallet refresh rate for {} {} to {}", getClass().getSimpleName(), getId(), getWalletRefreshPeriod()); 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 getWallet().startSyncing(getWalletRefreshPeriod()); // TODO (monero-project): wallet rpc waits until last sync period finishes before starting new sync period
} }
if (isPolling()) { if (isPollInProgress()) {
stopPolling(); stopPolling();
startPolling(); startPolling();
} }
@ -1920,7 +1922,7 @@ public abstract class Trade implements Tradable, Model {
private void startPolling() { private void startPolling() {
synchronized (walletLock) { synchronized (walletLock) {
if (isShutDownStarted || isPolling()) return; if (isShutDownStarted || isPollInProgress()) return;
log.info("Starting to poll wallet for {} {}", getClass().getSimpleName(), getId()); log.info("Starting to poll wallet for {} {}", getClass().getSimpleName(), getId());
txPollLooper = new TaskLooper(() -> pollWallet()); txPollLooper = new TaskLooper(() -> pollWallet());
txPollLooper.start(walletRefreshPeriodMs); txPollLooper.start(walletRefreshPeriodMs);
@ -1929,22 +1931,24 @@ public abstract class Trade implements Tradable, Model {
private void stopPolling() { private void stopPolling() {
synchronized (walletLock) { synchronized (walletLock) {
if (isPolling()) { if (isPollInProgress()) {
txPollLooper.stop(); txPollLooper.stop();
txPollLooper = null; txPollLooper = null;
} }
} }
} }
private boolean isPolling() { private boolean isPollInProgress() {
synchronized (walletLock) { synchronized (walletLock) {
return txPollLooper != null; return txPollLooper != null;
} }
} }
private void pollWallet() { private void pollWallet() {
try { if (pollInProgress) return;
synchronized (walletLock) { synchronized (pollLock) {
pollInProgress = true;
try {
// log warning if wallet is too far behind daemon // log warning if wallet is too far behind daemon
MoneroDaemonInfo lastInfo = xmrConnectionService.getLastInfo(); MoneroDaemonInfo lastInfo = xmrConnectionService.getLastInfo();
@ -2026,13 +2030,15 @@ public abstract class Trade implements Tradable, Model {
} }
} }
} }
} } catch (Exception e) {
} catch (Exception e) { boolean isWalletConnected = isWalletConnectedToDaemon();
boolean isWalletConnected = isWalletConnectedToDaemon(); if (!isWalletConnected) xmrConnectionService.checkConnection(); // check connection if wallet is not connected
if (!isWalletConnected) xmrConnectionService.checkConnection(); // check connection if wallet is not connected if (!isShutDownStarted && wallet != null && isWalletConnected) {
if (!isShutDownStarted && wallet != null && isWalletConnected) { e.printStackTrace();
e.printStackTrace(); log.warn("Error polling trade wallet for {} {}: {}. Monerod={}", getClass().getSimpleName(), getId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection());
log.warn("Error polling trade wallet for {} {}: {}. Monerod={}", getClass().getSimpleName(), getId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection()); }
} finally {
pollInProgress = false;
} }
} }
} }