cancel pending price request on select new provider

This commit is contained in:
woodser 2024-01-17 20:28:28 -05:00
parent 75e85179b4
commit 7beae49dd2
3 changed files with 29 additions and 7 deletions

View file

@ -143,7 +143,7 @@ public class PriceFeedService {
public void awaitExternalPrices() { public void awaitExternalPrices() {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> {
if (hasExternalPrices()) latch.countDown(); if (hasExternalPrices() && latch.getCount() != 0) latch.countDown();
}; };
updateCounter.addListener(listener); updateCounter.addListener(listener);
if (hasExternalPrices()) { if (hasExternalPrices()) {
@ -277,11 +277,13 @@ public class PriceFeedService {
// returns true if provider selection loops back to beginning // returns true if provider selection loops back to beginning
private boolean setNewPriceProvider() { private boolean setNewPriceProvider() {
httpClient.cancelPendingRequest();
boolean looped = providersRepository.selectNextProviderBaseUrl(); boolean looped = providersRepository.selectNextProviderBaseUrl();
if (!providersRepository.getBaseUrl().isEmpty()) if (!providersRepository.getBaseUrl().isEmpty()) {
priceProvider = new PriceProvider(httpClient, providersRepository.getBaseUrl()); priceProvider = new PriceProvider(httpClient, providersRepository.getBaseUrl());
else } else {
log.warn("We cannot create a new priceProvider because new base url is empty."); log.warn("We cannot create a new priceProvider because new base url is empty.");
}
return looped; return looped;
} }
@ -293,7 +295,7 @@ public class PriceFeedService {
} }
private void setHavenoMarketPrice(String currencyCode, Price price) { private void setHavenoMarketPrice(String currencyCode, Price price) {
UserThread.await(() -> { UserThread.execute(() -> {
synchronized (cache) { synchronized (cache) {
if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) { if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) {
cache.put(currencyCode, new MarketPrice(currencyCode, cache.put(currencyCode, new MarketPrice(currencyCode,
@ -374,7 +376,7 @@ public class PriceFeedService {
*/ */
public synchronized Map<String, MarketPrice> requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException { public synchronized Map<String, MarketPrice> requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { latch.countDown(); }; ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { if (latch.getCount() != 0) latch.countDown(); };
updateCounter.addListener(listener); updateCounter.addListener(listener);
requestAllPricesError = null; requestAllPricesError = null;
requestPrices(); requestPrices();
@ -442,7 +444,7 @@ public class PriceFeedService {
faultHandler.handleFault(errorMessage, new PriceRequestException(errorMessage)); faultHandler.handleFault(errorMessage, new PriceRequestException(errorMessage));
} }
UserThread.await(() -> updateCounter.set(updateCounter.get() + 1)); UserThread.execute(() -> updateCounter.set(updateCounter.get() + 1));
return result; return result;
} }

View file

@ -39,5 +39,7 @@ public interface HttpClient {
boolean hasPendingRequest(); boolean hasPendingRequest();
void cancelPendingRequest();
void shutDown(); void shutDown();
} }

View file

@ -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, private String requestWithoutProxy(String baseUrl,
String param, String param,
HttpMethod httpMethod, HttpMethod httpMethod,