private async Task <LatestFetch> Fetch() { var previous = _Latest; var fetch = new LatestFetch(); try { var rates = await _Inner.GetRatesAsync(); fetch.Latest = rates; fetch.Expiration = DateTimeOffset.UtcNow + ValidatyTime; fetch.NextRefresh = DateTimeOffset.UtcNow + RefreshRate; } catch (Exception ex) { if (previous != null) { fetch.Latest = previous.Latest; fetch.Expiration = previous.Expiration; } else { fetch.Expiration = DateTimeOffset.UtcNow; } fetch.NextRefresh = DateTimeOffset.UtcNow; fetch.Exception = ex; } _Latest = fetch; fetch.GetResult(); // Will throw if not valid return(fetch); }
public void LoadState(BackgroundFetcherState state) { if (state.LastRequested is DateTimeOffset lastRequested) { this.LastRequested = state.LastRequested; } if (state.LastUpdated is DateTimeOffset updated && state.Rates is List <BackgroundFetcherRate> rates) { var fetch = new LatestFetch() { Latest = rates.Select(r => new PairRate(r.Pair, r.BidAsk)).ToArray(), Updated = updated, NextRefresh = updated + RefreshRate, Expiration = updated + ValidatyTime }; _Latest = fetch; } }
public void LoadState(BackgroundFetcherState state) { if (ExchangeName != state.ExchangeName) { throw new InvalidOperationException("The state does not belong to this fetcher"); } if (state.LastRequested is DateTimeOffset lastRequested) { this.LastRequested = state.LastRequested; } if (state.LastUpdated is DateTimeOffset updated && state.Rates is List <BackgroundFetcherRate> rates) { var fetch = new LatestFetch() { Latest = rates.Select(r => new PairRate(r.Pair, r.BidAsk)).ToArray(), Updated = updated, NextRefresh = updated + RefreshRate, Expiration = updated + ValidatyTime }; _Latest = fetch; } }
private async Task <LatestFetch> Fetch(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var previous = _Latest; var fetch = new LatestFetch(); fetch.ExchangeName = ExchangeName; try { var rates = await _Inner.GetRatesAsync(cancellationToken); fetch.Latest = rates; fetch.Updated = DateTimeOffset.UtcNow; fetch.Expiration = fetch.Updated + ValidatyTime; fetch.NextRefresh = fetch.Updated + RefreshRate; } catch (Exception ex) { if (previous != null) { fetch.Latest = previous.Latest; fetch.Expiration = previous.Expiration; fetch.Backoff = previous.Backoff * 2; if (fetch.Backoff > MaxBackoff) { fetch.Backoff = MaxBackoff; } } else { fetch.Expiration = DateTimeOffset.UtcNow; } fetch.NextRefresh = DateTimeOffset.UtcNow + fetch.Backoff; fetch.Exception = ex; } _Latest = fetch; fetch.GetResult(); // Will throw if not valid return(fetch); }
private async Task <LatestFetch> Fetch() { var fetch = new LatestFetch(); try { var rates = await _Inner.GetRatesAsync(); fetch.Latest = rates; } catch (Exception ex) { fetch.Exception = ex; } fetch.Timestamp = DateTimeOffset.UtcNow; _Latest = fetch; if (fetch.Exception != null) { ExceptionDispatchInfo.Capture(fetch.Exception).Throw(); } return(fetch); }
public void InvalidateCache() { _Latest = null; }