Skip to content

Commit

Permalink
fix null price feed listener
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser authored Jul 1, 2024
1 parent 856faaf commit c01d634
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions core/src/main/java/haveno/core/provider/price/PriceFeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import haveno.core.monetary.TraditionalMoney;
import haveno.core.provider.PriceHttpClient;
import haveno.core.provider.ProvidersRepository;
import haveno.core.trade.HavenoUtils;
import haveno.core.trade.statistics.TradeStatistics3;
import haveno.core.user.Preferences;
import haveno.network.http.HttpClient;
Expand Down Expand Up @@ -144,15 +143,17 @@ public void requestPrices() {
public void awaitExternalPrices() {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> {
if (hasExternalPrices() && latch.getCount() != 0) latch.countDown();
if (hasExternalPrices()) UserThread.execute(() -> latch.countDown());
};
updateCounter.addListener(listener);
if (hasExternalPrices()) {
updateCounter.removeListener(listener);
return;
UserThread.execute(() -> updateCounter.addListener(listener));
if (hasExternalPrices()) UserThread.execute(() -> latch.countDown());
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
UserThread.execute(() -> updateCounter.removeListener(listener));
}
HavenoUtils.awaitLatch(latch);
updateCounter.removeListener(listener);
}

public boolean hasExternalPrices() {
Expand Down Expand Up @@ -377,17 +378,21 @@ public void applyLatestHavenoMarketPrice(Set<TradeStatistics3> tradeStatisticsSe
*/
public synchronized Map<String, MarketPrice> requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { if (latch.getCount() != 0) latch.countDown(); };
updateCounter.addListener(listener);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> latch.countDown();
UserThread.execute(() -> updateCounter.addListener(listener));
requestAllPricesError = null;
requestPrices();
UserThread.runAfter(() -> {
if (latch.getCount() == 0) return;
requestAllPricesError = "Timeout fetching market prices within 20 seconds";
latch.countDown();
if (latch.getCount() > 0) requestAllPricesError = "Timeout fetching market prices within 20 seconds";
UserThread.execute(() -> latch.countDown());
}, 20);
HavenoUtils.awaitLatch(latch);
updateCounter.removeListener(listener);
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
UserThread.execute(() -> updateCounter.removeListener(listener));
}
if (requestAllPricesError != null) throw new RuntimeException(requestAllPricesError);
return cache;
}
Expand Down

0 comments on commit c01d634

Please sign in to comment.