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() {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> 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<String, MarketPrice> requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException {
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);
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;
}

View File

@ -39,5 +39,7 @@ public interface HttpClient {
boolean hasPendingRequest();
void cancelPendingRequest();
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,
String param,
HttpMethod httpMethod,