Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip refresh if the previous refresh is ongoing #42

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/main/java/bisq/price/PriceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import org.slf4j.Logger;
Expand All @@ -36,6 +37,7 @@ public abstract class PriceProvider<T> implements SmartLifecycle, Supplier<T> {
protected final Logger log = LoggerFactory.getLogger(this.getClass());

private final Timer timer = new Timer(true);
private final AtomicBoolean isRefreshInProgress = new AtomicBoolean();

protected final Duration refreshInterval;

Expand All @@ -60,7 +62,11 @@ public final void start() {
// do the initial refresh asynchronously
UserThread.runAfter(() -> {
try {
refresh();
if (!isRefreshInProgress.get()) {
refresh();
} else {
log.debug("Refresh in progress. Skipping this iteration.");
}
} catch (Throwable t) {
log.warn("initial refresh failed", t);
}
Expand All @@ -70,7 +76,11 @@ public final void start() {
@Override
public void run() {
try {
refresh();
if (!isRefreshInProgress.get()) {
refresh();
} else {
log.debug("Refresh in progress. Skipping this iteration.");
}
} catch (Throwable t) {
// we only log scheduled calls to refresh that fail to ensure that
// the application does *not* halt, assuming the failure is temporary
Expand All @@ -82,13 +92,16 @@ public void run() {
}

private void refresh() {
long ts = System.currentTimeMillis();

put(doGet());

log.info("refresh took {} ms.", (System.currentTimeMillis() - ts));

onRefresh();
isRefreshInProgress.set(true);
try {
long ts = System.currentTimeMillis();
put(doGet());
log.info("refresh took {} ms.", (System.currentTimeMillis() - ts));
onRefresh();

} finally {
isRefreshInProgress.set(false);
}
}

protected abstract T doGet();
Expand Down