Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start pricenode providers asynchronously #4890

Merged
merged 2 commits into from Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pricenode/src/main/java/bisq/price/PriceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,17 +48,19 @@ public PriceProvider(Duration refreshInterval) {

@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
Expand Down
8 changes: 8 additions & 0 deletions pricenode/src/main/java/bisq/price/mining/FeeRateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
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.
*/
@Service
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}
Expand All @@ -56,6 +60,10 @@ public Map<String, Object> getFees() {
// 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public Map<String, Object> getAllMarketPrices() {
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,
Expand Down Expand Up @@ -136,6 +138,8 @@ private Map<String, ExchangeRate> getAggregateExchangeRates() {
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.junit.jupiter.api.Test;

import static java.lang.Thread.sleep;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -66,6 +67,9 @@ protected FeeRate doGet() {

// Initialize provider
dummyProvider.start();
try {
sleep(1000);
} catch (InterruptedException e) { }
dummyProvider.stop();

return dummyProvider;
Expand All @@ -86,6 +90,9 @@ protected FeeRate doGet() {

// Initialize provider
dummyProvider.start();
try {
sleep(1000);
} catch (InterruptedException e) { }
dummyProvider.stop();

return dummyProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -287,6 +288,9 @@ protected Set<ExchangeRate> doGet() {

// Initialize provider
dummyProvider.start();
try {
sleep(1000);
} catch (InterruptedException e) { }
dummyProvider.stop();

return dummyProvider;
Expand Down Expand Up @@ -322,6 +326,9 @@ protected Set<ExchangeRate> doGet() {

// Initialize provider
dummyProvider.start();
try {
sleep(1000);
} catch (InterruptedException e) { }
dummyProvider.stop();

return dummyProvider;
Expand Down