diff --git a/src/main/java/bisq/price/PriceProvider.java b/src/main/java/bisq/price/PriceProvider.java index f4e4981..dab1059 100644 --- a/src/main/java/bisq/price/PriceProvider.java +++ b/src/main/java/bisq/price/PriceProvider.java @@ -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; @@ -36,6 +37,7 @@ public abstract class PriceProvider implements SmartLifecycle, Supplier { 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; @@ -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); } @@ -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 @@ -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();