announce connection change on refresh period change

This commit is contained in:
woodser 2024-08-06 11:38:22 -04:00
parent fa25843684
commit d300ce8ce7
3 changed files with 35 additions and 13 deletions

View file

@ -101,6 +101,7 @@ public final class XmrConnectionService {
private Long lastLogPollErrorTimestamp; private Long lastLogPollErrorTimestamp;
private Long syncStartHeight = null; private Long syncStartHeight = null;
private TaskLooper daemonPollLooper; private TaskLooper daemonPollLooper;
private long lastRefreshPeriodMs = 0;
@Getter @Getter
private boolean isShutDownStarted; private boolean isShutDownStarted;
private List<MoneroConnectionManagerListener> listeners = new ArrayList<>(); private List<MoneroConnectionManagerListener> listeners = new ArrayList<>();
@ -353,7 +354,11 @@ public final class XmrConnectionService {
} }
public long getRefreshPeriodMs() { public long getRefreshPeriodMs() {
return connectionList.getRefreshPeriod() > 0 ? connectionList.getRefreshPeriod() : getDefaultRefreshPeriodMs(); return connectionList.getRefreshPeriod() > 0 ? connectionList.getRefreshPeriod() : getDefaultRefreshPeriodMs(false);
}
private long getInternalRefreshPeriodMs() {
return connectionList.getRefreshPeriod() > 0 ? connectionList.getRefreshPeriod() : getDefaultRefreshPeriodMs(true);
} }
public void verifyConnection() { public void verifyConnection() {
@ -423,12 +428,16 @@ public final class XmrConnectionService {
return connection != null && HavenoUtils.isLocalHost(connection.getUri()); return connection != null && HavenoUtils.isLocalHost(connection.getUri());
} }
private long getDefaultRefreshPeriodMs() { private long getDefaultRefreshPeriodMs(boolean internal) {
MoneroRpcConnection connection = getConnection(); MoneroRpcConnection connection = getConnection();
if (connection == null) return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; if (connection == null) return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS;
if (isConnectionLocalHost(connection)) { if (isConnectionLocalHost(connection)) {
if (lastInfo != null && (lastInfo.isBusySyncing() || (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight()))) return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped if (internal) return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS;
else return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing if (lastInfo != null && (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight())) {
return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped
} else {
return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing
}
} else if (isProxyApplied(connection)) { } else if (isProxyApplied(connection)) {
return REFRESH_PERIOD_ONION_MS; return REFRESH_PERIOD_ONION_MS;
} else { } else {
@ -648,7 +657,7 @@ public final class XmrConnectionService {
// update polling // update polling
doPollDaemon(); doPollDaemon();
if (currentConnection != getConnection()) return; // polling can change connection if (currentConnection != getConnection()) return; // polling can change connection
UserThread.runAfter(() -> updatePolling(), getRefreshPeriodMs() / 1000); UserThread.runAfter(() -> updatePolling(), getInternalRefreshPeriodMs() / 1000);
// notify listeners in parallel // notify listeners in parallel
log.info("XmrConnectionService.onConnectionChanged() uri={}, connected={}", currentConnection == null ? null : currentConnection.getUri(), currentConnection == null ? "false" : isConnected); log.info("XmrConnectionService.onConnectionChanged() uri={}, connected={}", currentConnection == null ? null : currentConnection.getUri(), currentConnection == null ? "false" : isConnected);
@ -668,7 +677,7 @@ public final class XmrConnectionService {
synchronized (lock) { synchronized (lock) {
if (daemonPollLooper != null) daemonPollLooper.stop(); if (daemonPollLooper != null) daemonPollLooper.stop();
daemonPollLooper = new TaskLooper(() -> pollDaemon()); daemonPollLooper = new TaskLooper(() -> pollDaemon());
daemonPollLooper.start(getRefreshPeriodMs()); daemonPollLooper.start(getInternalRefreshPeriodMs());
} }
} }
@ -725,6 +734,13 @@ public final class XmrConnectionService {
// connected to daemon // connected to daemon
isConnected = true; isConnected = true;
// announce connection change if refresh period changes
if (getRefreshPeriodMs() != lastRefreshPeriodMs) {
lastRefreshPeriodMs = getRefreshPeriodMs();
onConnectionChanged(getConnection()); // causes new poll
return;
}
// update properties on user thread // update properties on user thread
UserThread.execute(() -> { UserThread.execute(() -> {

View file

@ -2350,7 +2350,10 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model {
// check if ignored // check if ignored
if (isShutDownStarted) return; if (isShutDownStarted) return;
if (getWallet() == null) return; if (getWallet() == null) return;
if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return; if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) {
updatePollPeriod();
return;
}
// set daemon connection (must restart monero-wallet-rpc if proxy uri changed) // set daemon connection (must restart monero-wallet-rpc if proxy uri changed)
String oldProxyUri = wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri(); String oldProxyUri = wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri();

View file

@ -1598,12 +1598,15 @@ public class XmrWalletService extends XmrWalletBase {
// check if ignored // check if ignored
if (wallet == null || isShutDownStarted) return; if (wallet == null || isShutDownStarted) return;
if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return; if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) {
updatePollPeriod();
return;
}
// update connection
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, monerod={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri); log.info("Setting daemon connection for main wallet, monerod={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri);
// update connection
if (wallet instanceof MoneroWalletRpc) { if (wallet instanceof MoneroWalletRpc) {
if (StringUtils.equals(oldProxyUri, newProxyUri)) { if (StringUtils.equals(oldProxyUri, newProxyUri)) {
wallet.setDaemonConnection(connection); wallet.setDaemonConnection(connection);
@ -1722,14 +1725,14 @@ public class XmrWalletService extends XmrWalletBase {
public void updatePollPeriod() { public void updatePollPeriod() {
if (isShutDownStarted) return; if (isShutDownStarted) return;
setPollPeriod(getPollPeriod()); setPollPeriodMs(getPollPeriodMs());
} }
private long getPollPeriod() { private long getPollPeriodMs() {
return xmrConnectionService.getRefreshPeriodMs(); return xmrConnectionService.getRefreshPeriodMs();
} }
private void setPollPeriod(long pollPeriodMs) { private void setPollPeriodMs(long pollPeriodMs) {
synchronized (walletLock) { synchronized (walletLock) {
if (this.isShutDownStarted) return; if (this.isShutDownStarted) return;
if (this.pollPeriodMs != null && this.pollPeriodMs == pollPeriodMs) return; if (this.pollPeriodMs != null && this.pollPeriodMs == pollPeriodMs) return;