remove dedicated connection change thread for trade

This commit is contained in:
woodser 2024-07-19 08:49:10 -04:00
parent 82d586ab78
commit 5d739f912c
3 changed files with 29 additions and 27 deletions

View file

@ -303,7 +303,7 @@ public final class XmrConnectionService {
// switch to best connection // switch to best connection
if (bestConnection == null) { if (bestConnection == null) {
log.warn("Could not get connection to switch to"); log.warn("No connection to switch to");
return false; return false;
} }
setConnection(bestConnection); setConnection(bestConnection);

View file

@ -627,7 +627,7 @@ public abstract class Trade implements Tradable, Model {
// handle connection change on dedicated thread // handle connection change on dedicated thread
xmrConnectionService.addConnectionListener(connection -> { xmrConnectionService.addConnectionListener(connection -> {
ThreadUtils.execute(() -> onConnectionChanged(connection), getConnectionChangedThreadId()); ThreadUtils.execute(() -> onConnectionChanged(connection), getId());
}); });
// reset buyer's payment sent state if no ack receive // reset buyer's payment sent state if no ack receive
@ -1501,7 +1501,6 @@ public abstract class Trade implements Tradable, Model {
isShutDown = true; isShutDown = true;
List<Runnable> shutDownThreads = new ArrayList<>(); List<Runnable> shutDownThreads = new ArrayList<>();
shutDownThreads.add(() -> ThreadUtils.shutDown(getId())); shutDownThreads.add(() -> ThreadUtils.shutDown(getId()));
shutDownThreads.add(() -> ThreadUtils.shutDown(getConnectionChangedThreadId()));
ThreadUtils.awaitTasks(shutDownThreads); ThreadUtils.awaitTasks(shutDownThreads);
} }
@ -2326,10 +2325,6 @@ public abstract class Trade implements Tradable, Model {
return tradeVolumeProperty; return tradeVolumeProperty;
} }
private String getConnectionChangedThreadId() {
return getId() + ".onConnectionChanged";
}
private void onConnectionChanged(MoneroRpcConnection connection) { private void onConnectionChanged(MoneroRpcConnection connection) {
synchronized (walletLock) { synchronized (walletLock) {
@ -2399,6 +2394,7 @@ public abstract class Trade implements Tradable, Model {
} }
private void syncWallet(boolean pollWallet) { private void syncWallet(boolean pollWallet) {
try {
if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId()); if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId());
if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId()); if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId());
if (isWalletBehind()) { if (isWalletBehind()) {
@ -2417,6 +2413,10 @@ public abstract class Trade implements Tradable, Model {
} }
if (pollWallet) pollWallet(); if (pollWallet) pollWallet();
} catch (Exception e) {
ThreadUtils.execute(() -> requestSwitchToNextBestConnection(), getId());
throw e;
}
} }
public void updatePollPeriod() { public void updatePollPeriod() {
@ -2593,7 +2593,7 @@ public abstract class Trade implements Tradable, Model {
boolean isWalletConnected = isWalletConnectedToDaemon(); boolean isWalletConnected = isWalletConnectedToDaemon();
if (wallet != null && !isShutDownStarted && isWalletConnected) { if (wallet != null && !isShutDownStarted && isWalletConnected) {
log.warn("Error polling trade wallet for {} {}, errorMessage={}. Monerod={}", getClass().getSimpleName(), getShortId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection()); log.warn("Error polling trade wallet for {} {}, errorMessage={}. Monerod={}", getClass().getSimpleName(), getShortId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection());
requestSwitchToNextBestConnection(); ThreadUtils.execute(() -> requestSwitchToNextBestConnection(), getId()); // do not block polling thread
//e.printStackTrace(); //e.printStackTrace();
} }
} }

View file

@ -1285,7 +1285,9 @@ public class XmrWalletService {
else log.info(appliedMsg); else log.info(appliedMsg);
// listen for connection changes // listen for connection changes
xmrConnectionService.addConnectionListener(connection -> ThreadUtils.execute(() -> onConnectionChanged(connection), THREAD_ID)); xmrConnectionService.addConnectionListener(connection -> ThreadUtils.execute(() -> {
onConnectionChanged(connection);
}, THREAD_ID));
// initialize main wallet when daemon synced // initialize main wallet when daemon synced
walletInitListener = (obs, oldVal, newVal) -> initMainWalletIfConnected(); walletInitListener = (obs, oldVal, newVal) -> initMainWalletIfConnected();
@ -1669,7 +1671,7 @@ public class XmrWalletService {
if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return; if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return;
String oldProxyUri = wallet == null || wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri(); String oldProxyUri = wallet == null || wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri();
String newProxyUri = connection == null ? null : connection.getProxyUri(); String newProxyUri = connection == null ? null : connection.getProxyUri();
log.info("Setting daemon connection for main wallet: uri={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri); log.info("Setting daemon connection for main wallet, monerod={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri);
// force restart main wallet if connection changed before synced // force restart main wallet if connection changed before synced
if (!wasWalletSynced) { if (!wasWalletSynced) {
@ -1706,7 +1708,7 @@ public class XmrWalletService {
updatePollPeriod(); updatePollPeriod();
} }
log.info("Done setting main wallet monerod=" + (wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getUri())); log.info("Done setting daemon connection for main wallet, monerod=" + (wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getUri()));
} }
} }