| 1 |
using System.Diagnostics; |
| 2 |
using Blog.Models; |
| 3 |
using Microsoft.Extensions.Options; |
| 4 |
|
| 5 |
namespace Blog.Services; |
| 6 |
|
| 7 |
|
| 8 |
public sealed class WarframeOptions |
| 9 |
{ |
| 10 |
public const string Section = "Warframe"; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public string DropTablesUrl { get; set; } = "https://www.warframe.com/droptables"; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public TimeSpan CacheFor { get; set; } = TimeSpan.FromHours(12); |
| 22 |
|
| 23 |
|
| 24 |
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public sealed class WarframeDropService( |
| 42 |
IHttpClientFactory clients, |
| 43 |
IOptions<WarframeOptions> options, |
| 44 |
ILogger<WarframeDropService> logger) |
| 45 |
{ |
| 46 |
|
| 47 |
public const string ClientName = "warframe"; |
| 48 |
|
| 49 |
private readonly WarframeOptions _options = options.Value; |
| 50 |
private readonly SemaphoreSlim _gate = new(1, 1); |
| 51 |
|
| 52 |
private DropTables? _tables; |
| 53 |
private DateTimeOffset _expiresAt = DateTimeOffset.MinValue; |
| 54 |
|
| 55 |
public async Task<DropTableStatus> GetTablesAsync(CancellationToken cancellationToken = default) |
| 56 |
{ |
| 57 |
if (_tables is { } fresh && DateTimeOffset.UtcNow < _expiresAt) |
| 58 |
{ |
| 59 |
return DropTableStatus.Ready(fresh); |
| 60 |
} |
| 61 |
|
| 62 |
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 63 |
try |
| 64 |
{ |
| 65 |
|
| 66 |
if (_tables is { } current && DateTimeOffset.UtcNow < _expiresAt) |
| 67 |
{ |
| 68 |
return DropTableStatus.Ready(current); |
| 69 |
} |
| 70 |
|
| 71 |
var tables = await FetchAsync(cancellationToken).ConfigureAwait(false); |
| 72 |
_tables = tables; |
| 73 |
_expiresAt = DateTimeOffset.UtcNow + _options.CacheFor; |
| 74 |
return DropTableStatus.Ready(tables); |
| 75 |
} |
| 76 |
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException or TimeoutException) |
| 77 |
{ |
| 78 |
logger.LogWarning(ex, "Could not read the Warframe drop tables from {Url}", _options.DropTablesUrl); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
_expiresAt = DateTimeOffset.UtcNow + TimeSpan.FromMinutes(5); |
| 83 |
|
| 84 |
return DropTableStatus.Failed( |
| 85 |
$"Could not reach {_options.DropTablesUrl} ({ex.Message})", |
| 86 |
_tables); |
| 87 |
} |
| 88 |
finally |
| 89 |
{ |
| 90 |
_gate.Release(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private async Task<DropTables> FetchAsync(CancellationToken cancellationToken) |
| 95 |
{ |
| 96 |
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| 97 |
timeout.CancelAfter(_options.Timeout); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
using var client = clients.CreateClient(ClientName); |
| 102 |
|
| 103 |
var stopwatch = Stopwatch.StartNew(); |
| 104 |
var html = await client.GetStringAsync(_options.DropTablesUrl, timeout.Token).ConfigureAwait(false); |
| 105 |
var downloaded = stopwatch.ElapsedMilliseconds; |
| 106 |
|
| 107 |
var tables = DropTableParser.Parse(html, DateTimeOffset.UtcNow); |
| 108 |
|
| 109 |
logger.LogInformation( |
| 110 |
"Read the Warframe drop tables ({Update}): {Bytes} bytes in {Downloaded} ms, {Items} items parsed in {Parsed} ms", |
| 111 |
tables.LastUpdate, html.Length, downloaded, tables.Names.Count, stopwatch.ElapsedMilliseconds - downloaded); |
| 112 |
|
| 113 |
return tables; |
| 114 |
} |
| 115 |
} |