Report if BackgroundFetcherRateProvider has expired entry

This commit is contained in:
nicolas.dorier 2018-08-25 15:09:42 +09:00
parent 13f21aa0d6
commit 0edaedb6ab
9 changed files with 47 additions and 9 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
<Version>1.0.2.97</Version> <Version>1.0.2.98</Version>
<NoWarn>NU1701,CA1816,CA1308,CA1810,CA2208</NoWarn> <NoWarn>NU1701,CA1816,CA1308,CA1810,CA2208</NoWarn>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>

View File

@ -1,9 +1,11 @@
using System; using System;
using Microsoft.Extensions.Logging;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Logging;
using BTCPayServer.Rating; using BTCPayServer.Rating;
namespace BTCPayServer.Services.Rates namespace BTCPayServer.Services.Rates
@ -73,7 +75,20 @@ namespace BTCPayServer.Services.Rates
LatestFetch _Latest; LatestFetch _Latest;
public async Task<ExchangeRates> GetRatesAsync() public async Task<ExchangeRates> GetRatesAsync()
{ {
return (_Latest ?? (await Fetch())).GetResult(); var latest = _Latest;
if(latest != null && latest.Expiration <= DateTimeOffset.UtcNow + TimeSpan.FromSeconds(1.0))
{
Logs.PayServer.LogWarning($"GetRatesAsync was called on {GetExchangeName()} when the rate is outdated. It should never happen, let BTCPayServer developers know about this.");
latest = null;
}
return (latest ?? (await Fetch())).GetResult();
}
private string GetExchangeName()
{
if (_Inner is IHasExchangeName exchangeName)
return exchangeName.ExchangeName ?? "???";
return "???";
} }
private async Task<LatestFetch> Fetch() private async Task<LatestFetch> Fetch()

View File

@ -9,7 +9,7 @@ using BTCPayServer.Rating;
namespace BTCPayServer.Services.Rates namespace BTCPayServer.Services.Rates
{ {
public class BitpayRateProvider : IRateProvider public class BitpayRateProvider : IRateProvider, IHasExchangeName
{ {
public const string BitpayName = "bitpay"; public const string BitpayName = "bitpay";
Bitpay _Bitpay; Bitpay _Bitpay;
@ -20,6 +20,8 @@ namespace BTCPayServer.Services.Rates
_Bitpay = bitpay; _Bitpay = bitpay;
} }
public string ExchangeName => BitpayName;
public async Task<ExchangeRates> GetRatesAsync() public async Task<ExchangeRates> GetRatesAsync()
{ {
return new ExchangeRates((await _Bitpay.GetRatesAsync().ConfigureAwait(false)) return new ExchangeRates((await _Bitpay.GetRatesAsync().ConfigureAwait(false))

View File

@ -7,7 +7,7 @@ using Microsoft.Extensions.Caching.Memory;
namespace BTCPayServer.Services.Rates namespace BTCPayServer.Services.Rates
{ {
public class CachedRateProvider : IRateProvider public class CachedRateProvider : IRateProvider, IHasExchangeName
{ {
private IRateProvider _Inner; private IRateProvider _Inner;
private IMemoryCache _MemoryCache; private IMemoryCache _MemoryCache;
@ -31,7 +31,7 @@ namespace BTCPayServer.Services.Rates
} }
} }
public string ExchangeName { get; set; } public string ExchangeName { get; }
public TimeSpan CacheSpan public TimeSpan CacheSpan
{ {

View File

@ -49,7 +49,7 @@ namespace BTCPayServer.Services.Rates
Task AddHeader(HttpRequestMessage message); Task AddHeader(HttpRequestMessage message);
} }
public class CoinAverageRateProvider : IRateProvider public class CoinAverageRateProvider : IRateProvider, IHasExchangeName
{ {
public const string CoinAverageName = "coinaverage"; public const string CoinAverageName = "coinaverage";
public CoinAverageRateProvider() public CoinAverageRateProvider()
@ -82,6 +82,8 @@ namespace BTCPayServer.Services.Rates
public ICoinAverageAuthenticator Authenticator { get; set; } public ICoinAverageAuthenticator Authenticator { get; set; }
public string ExchangeName => Exchange ?? CoinAverageName;
private bool TryToBidAsk(JProperty p, out BidAsk bidAsk) private bool TryToBidAsk(JProperty p, out BidAsk bidAsk)
{ {
bidAsk = null; bidAsk = null;

View File

@ -10,7 +10,7 @@ using ExchangeSharp;
namespace BTCPayServer.Services.Rates namespace BTCPayServer.Services.Rates
{ {
public class ExchangeSharpRateProvider : IRateProvider public class ExchangeSharpRateProvider : IRateProvider, IHasExchangeName
{ {
readonly ExchangeAPI _ExchangeAPI; readonly ExchangeAPI _ExchangeAPI;
readonly string _ExchangeName; readonly string _ExchangeName;
@ -29,6 +29,8 @@ namespace BTCPayServer.Services.Rates
get; set; get; set;
} }
public string ExchangeName => _ExchangeName;
public async Task<ExchangeRates> GetRatesAsync() public async Task<ExchangeRates> GetRatesAsync()
{ {
await new SynchronizationContextRemover(); await new SynchronizationContextRemover();

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BTCPayServer.Services.Rates
{
public interface IHasExchangeName
{
string ExchangeName { get; }
}
}

View File

@ -13,7 +13,7 @@ using Newtonsoft.Json.Linq;
namespace BTCPayServer.Services.Rates namespace BTCPayServer.Services.Rates
{ {
// Make sure that only one request is sent to kraken in general // Make sure that only one request is sent to kraken in general
public class KrakenExchangeRateProvider : IRateProvider public class KrakenExchangeRateProvider : IRateProvider, IHasExchangeName
{ {
public KrakenExchangeRateProvider() public KrakenExchangeRateProvider()
{ {
@ -31,6 +31,9 @@ namespace BTCPayServer.Services.Rates
_LocalClient = null; _LocalClient = null;
} }
} }
public string ExchangeName => "kraken";
HttpClient _LocalClient; HttpClient _LocalClient;
static HttpClient _Client = new HttpClient(); static HttpClient _Client = new HttpClient();

View File

@ -9,11 +9,13 @@ using Newtonsoft.Json.Linq;
namespace BTCPayServer.Services.Rates namespace BTCPayServer.Services.Rates
{ {
public class QuadrigacxRateProvider : IRateProvider public class QuadrigacxRateProvider : IRateProvider, IHasExchangeName
{ {
public const string QuadrigacxName = "quadrigacx"; public const string QuadrigacxName = "quadrigacx";
static HttpClient _Client = new HttpClient(); static HttpClient _Client = new HttpClient();
public string ExchangeName => QuadrigacxName;
private bool TryToBidAsk(JObject p, out BidAsk v) private bool TryToBidAsk(JObject p, out BidAsk v)
{ {
v = null; v = null;