Skip to content

Commit

Permalink
update wallet rpc poll period on startSyncing()
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Aug 6, 2022
1 parent e49a6fb commit fffe082
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
69 changes: 44 additions & 25 deletions src/main/java/monero/common/TaskLooper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class TaskLooper {

private Runnable task;
private long periodInMs;
private boolean isStarted;
private boolean isLooping;

Expand All @@ -26,39 +27,57 @@ public TaskLooper(Runnable task) {
* @param periodInMs the loop period in milliseconds
*/
public synchronized void start(long periodInMs) {
isStarted = true;
if (isLooping) return;

// start looping
isLooping = true;
Thread loop = new Thread(new Runnable() {
@Override
public void run() {
while (isStarted && !Thread.currentThread().isInterrupted()) {

// run the task
long startTime = System.currentTimeMillis();
task.run();

// wait remaining period
if (isStarted) {
try { TimeUnit.MILLISECONDS.sleep(periodInMs - (System.currentTimeMillis() - startTime)); } // target fixed period by accounting for run time
catch (Exception e) {
isLooping = false;
if (isStarted) throw new RuntimeException(e);
synchronized (this) {
this.periodInMs = periodInMs;
if (isStarted) return;
isStarted = true;

// start looping
if (isLooping) return;
isLooping = true;
TaskLooper that = this;
Thread loop = new Thread(new Runnable() {
@Override
public void run() {
while (isStarted && !Thread.currentThread().isInterrupted()) {

// run the task
long startTime = System.currentTimeMillis();
task.run();

// wait remaining period
if (isStarted) {
try { TimeUnit.MILLISECONDS.sleep(that.periodInMs - (System.currentTimeMillis() - startTime)); } // target fixed period by accounting for run time
catch (Exception e) {
isLooping = false;
if (isStarted) throw new RuntimeException(e);
}
}
}
isLooping = false;
}
isLooping = false;
}
});
loop.start();
});
loop.start();
}
}

/**
* Stop the task loop.
*/
public void stop() {
isStarted = false;
synchronized (this) {
isStarted = false;
}
}

/**
* Set the loop period in milliseconds.
*
* @param periodInMs the loop period in milliseconds
*/
public void setPeriodInMs(long periodInMs) {
synchronized (this) {
this.periodInMs = periodInMs;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/monero/wallet/MoneroWalletRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ public void startSyncing(Long syncPeriodInMs) {

// update sync period for poller
this.syncPeriodInMs = syncPeriodInSeconds * 1000;
if (walletPoller != null) walletPoller.setPeriodInMs(syncPeriodInMs);

// poll if listening
poll();
Expand Down Expand Up @@ -2300,6 +2301,10 @@ public void setIsPolling(boolean isPolling) {
else looper.stop();
}

public void setPeriodInMs(long periodInMs) {
looper.setPeriodInMs(periodInMs);
}

public void poll() {
try {

Expand Down

0 comments on commit fffe082

Please sign in to comment.