mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 18:03:12 +01:00
Merge pull request #4890 from jmacxx/fix_pricenode
Start pricenode providers asynchronously
This commit is contained in:
commit
dc3811749c
@ -17,12 +17,15 @@
|
||||
|
||||
package bisq.price;
|
||||
|
||||
import bisq.common.UserThread;
|
||||
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@ -45,17 +48,19 @@ public abstract class PriceProvider<T> implements SmartLifecycle, Supplier<T> {
|
||||
|
||||
@Override
|
||||
public final T get() {
|
||||
if (!isRunning())
|
||||
throw new IllegalStateException("call start() before calling get()");
|
||||
|
||||
return cachedResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void start() {
|
||||
// we call refresh outside the context of a timer once at startup to ensure that
|
||||
// any exceptions thrown get propagated and cause the application to halt
|
||||
refresh();
|
||||
// do the initial refresh asynchronously
|
||||
UserThread.runAfter(() -> {
|
||||
try {
|
||||
refresh();
|
||||
} catch (Throwable t) {
|
||||
log.warn("initial refresh failed", t);
|
||||
}
|
||||
}, 1, TimeUnit.MILLISECONDS);
|
||||
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
|
@ -27,6 +27,9 @@ import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* High-level mining {@link FeeRate} operations.
|
||||
*/
|
||||
@ -34,6 +37,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
class FeeRateService {
|
||||
|
||||
private final List<FeeRateProvider> providers;
|
||||
protected final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
/**
|
||||
* Construct a {@link FeeRateService} with a list of all {@link FeeRateProvider}
|
||||
@ -56,6 +60,10 @@ class FeeRateService {
|
||||
// Process each provider, retrieve and store their fee rate
|
||||
providers.forEach(p -> {
|
||||
FeeRate feeRate = p.get();
|
||||
if (feeRate == null) {
|
||||
log.warn("feeRate is null, provider={} ", p.toString());
|
||||
return;
|
||||
}
|
||||
String currency = feeRate.getCurrency();
|
||||
if ("BTC".equals(currency)) {
|
||||
sumOfAllFeeRates.getAndAdd(feeRate.getPrice());
|
||||
|
@ -61,6 +61,8 @@ class ExchangeRateService {
|
||||
Map<String, ExchangeRate> aggregateExchangeRates = getAggregateExchangeRates();
|
||||
|
||||
providers.forEach(p -> {
|
||||
if (p.get() == null)
|
||||
return;
|
||||
Set<ExchangeRate> exchangeRates = p.get();
|
||||
|
||||
// Specific metadata fields for specific providers are expected by the client,
|
||||
@ -136,6 +138,8 @@ class ExchangeRateService {
|
||||
private Map<String, List<ExchangeRate>> getCurrencyCodeToExchangeRates() {
|
||||
Map<String, List<ExchangeRate>> currencyCodeToExchangeRates = new HashMap<>();
|
||||
for (ExchangeRateProvider p : providers) {
|
||||
if (p.get() == null)
|
||||
continue;
|
||||
for (ExchangeRate exchangeRate : p.get()) {
|
||||
String currencyCode = exchangeRate.getCurrency();
|
||||
if (currencyCodeToExchangeRates.containsKey(currencyCode)) {
|
||||
|
@ -28,6 +28,7 @@ import java.time.Instant;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
@ -66,6 +67,9 @@ public class MempoolFeeRateProviderTest {
|
||||
|
||||
// Initialize provider
|
||||
dummyProvider.start();
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) { }
|
||||
dummyProvider.stop();
|
||||
|
||||
return dummyProvider;
|
||||
@ -86,6 +90,9 @@ public class MempoolFeeRateProviderTest {
|
||||
|
||||
// Initialize provider
|
||||
dummyProvider.start();
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) { }
|
||||
dummyProvider.stop();
|
||||
|
||||
return dummyProvider;
|
||||
|
@ -44,6 +44,7 @@ import ch.qos.logback.core.read.ListAppender;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
@ -287,6 +288,9 @@ public class ExchangeRateServiceTest {
|
||||
|
||||
// Initialize provider
|
||||
dummyProvider.start();
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) { }
|
||||
dummyProvider.stop();
|
||||
|
||||
return dummyProvider;
|
||||
@ -322,6 +326,9 @@ public class ExchangeRateServiceTest {
|
||||
|
||||
// Initialize provider
|
||||
dummyProvider.start();
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) { }
|
||||
dummyProvider.stop();
|
||||
|
||||
return dummyProvider;
|
||||
|
Loading…
Reference in New Issue
Block a user