From 7beae49dd2f43937c2c1334bfe4c6167fd9e1249 Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 17 Jan 2024 20:28:28 -0500 Subject: [PATCH] cancel pending price request on select new provider --- .../core/provider/price/PriceFeedService.java | 16 +++++++++------- .../java/haveno/network/http/HttpClient.java | 2 ++ .../haveno/network/http/HttpClientImpl.java | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/haveno/core/provider/price/PriceFeedService.java b/core/src/main/java/haveno/core/provider/price/PriceFeedService.java index 28cab492..eca0c8fb 100644 --- a/core/src/main/java/haveno/core/provider/price/PriceFeedService.java +++ b/core/src/main/java/haveno/core/provider/price/PriceFeedService.java @@ -143,7 +143,7 @@ public class PriceFeedService { public void awaitExternalPrices() { CountDownLatch latch = new CountDownLatch(1); ChangeListener listener = (observable, oldValue, newValue) -> { - if (hasExternalPrices()) latch.countDown(); + if (hasExternalPrices() && latch.getCount() != 0) latch.countDown(); }; updateCounter.addListener(listener); if (hasExternalPrices()) { @@ -277,12 +277,14 @@ public class PriceFeedService { // returns true if provider selection loops back to beginning private boolean setNewPriceProvider() { + httpClient.cancelPendingRequest(); boolean looped = providersRepository.selectNextProviderBaseUrl(); - if (!providersRepository.getBaseUrl().isEmpty()) + if (!providersRepository.getBaseUrl().isEmpty()) { priceProvider = new PriceProvider(httpClient, providersRepository.getBaseUrl()); - else + } else { log.warn("We cannot create a new priceProvider because new base url is empty."); - return looped; + } + return looped; } @Nullable @@ -293,7 +295,7 @@ public class PriceFeedService { } private void setHavenoMarketPrice(String currencyCode, Price price) { - UserThread.await(() -> { + UserThread.execute(() -> { synchronized (cache) { if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) { cache.put(currencyCode, new MarketPrice(currencyCode, @@ -374,7 +376,7 @@ public class PriceFeedService { */ public synchronized Map requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException { CountDownLatch latch = new CountDownLatch(1); - ChangeListener listener = (observable, oldValue, newValue) -> { latch.countDown(); }; + ChangeListener listener = (observable, oldValue, newValue) -> { if (latch.getCount() != 0) latch.countDown(); }; updateCounter.addListener(listener); requestAllPricesError = null; requestPrices(); @@ -442,7 +444,7 @@ public class PriceFeedService { faultHandler.handleFault(errorMessage, new PriceRequestException(errorMessage)); } - UserThread.await(() -> updateCounter.set(updateCounter.get() + 1)); + UserThread.execute(() -> updateCounter.set(updateCounter.get() + 1)); return result; } diff --git a/p2p/src/main/java/haveno/network/http/HttpClient.java b/p2p/src/main/java/haveno/network/http/HttpClient.java index 7027ddae..92add888 100644 --- a/p2p/src/main/java/haveno/network/http/HttpClient.java +++ b/p2p/src/main/java/haveno/network/http/HttpClient.java @@ -39,5 +39,7 @@ public interface HttpClient { boolean hasPendingRequest(); + void cancelPendingRequest(); + void shutDown(); } diff --git a/p2p/src/main/java/haveno/network/http/HttpClientImpl.java b/p2p/src/main/java/haveno/network/http/HttpClientImpl.java index 17185d60..b28072ef 100644 --- a/p2p/src/main/java/haveno/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/haveno/network/http/HttpClientImpl.java @@ -135,6 +135,24 @@ public class HttpClientImpl implements HttpClient { } } + public void cancelPendingRequest() { + if (!hasPendingRequest) return; + try { + if (connection != null) { + connection.getInputStream().close(); + connection.disconnect(); + connection = null; + } + if (closeableHttpClient != null) { + closeableHttpClient.close(); + closeableHttpClient = null; + } + } catch (IOException err) { + // igbnore + } + hasPendingRequest = false; + } + private String requestWithoutProxy(String baseUrl, String param, HttpMethod httpMethod,