House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -89,7 +89,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
// The networks a published list holds; an entry that is not an address or a network is an error, never skipped.
|
||||
public static List<IPNetwork> Parse(CdnListFormat format, string body)
|
||||
{
|
||||
IEnumerable<string> entries = format switch
|
||||
var entries = format switch
|
||||
{
|
||||
CdnListFormat.Lines => body.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Where(l => !l.StartsWith('#')),
|
||||
CdnListFormat.StringArray => Root(body),
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
return default;
|
||||
if (address.IsIPv4MappedToIPv6)
|
||||
address = address.MapToIPv4();
|
||||
Dictionary<string, (CdnProvider Provider, IPNetwork[] Networks, DateTime FetchedAt)> sets;
|
||||
var sets = default(Dictionary<string, (CdnProvider Provider, IPNetwork[] Networks, DateTime FetchedAt)>);
|
||||
lock (_gate)
|
||||
sets = _sets;
|
||||
foreach (var (provider, networks, _) in sets.Values)
|
||||
|
||||
@@ -12,7 +12,11 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
// Keeps the CDNs' published address ranges current by itself (owner decision 2026-10-04): at start it loads the stored
|
||||
// ones, then once a day it downloads each list again. A list that fails to download or to parse, or comes back empty,
|
||||
// leaves the stored one in place and is tried the next day. Like GeoUpdater, plain HTTPS to fixed hosts, not federation.
|
||||
public sealed class CdnUpdater : BackgroundService
|
||||
public sealed class CdnUpdater(
|
||||
IHttpClientFactory http,
|
||||
IOptionsMonitor<StatisticsOptions> options,
|
||||
ICdnRanges cdnRanges,
|
||||
ILogger<CdnUpdater> logger) : BackgroundService
|
||||
{
|
||||
public const string ClientName = "cdn";
|
||||
static readonly TimeSpan FirstWait = TimeSpan.FromMinutes(1);
|
||||
@@ -20,28 +24,16 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
const int MaxBytes = 16 * 1024 * 1024;
|
||||
const int MaxRanges = 200_000;
|
||||
|
||||
readonly IHttpClientFactory _http;
|
||||
readonly IOptionsMonitor<StatisticsOptions> _options;
|
||||
readonly ICdnRanges _ranges;
|
||||
readonly ILogger<CdnUpdater> _logger;
|
||||
|
||||
public CdnUpdater(IHttpClientFactory http, IOptionsMonitor<StatisticsOptions> options, ICdnRanges ranges, ILogger<CdnUpdater> logger)
|
||||
{
|
||||
_http = http;
|
||||
_options = options;
|
||||
_ranges = ranges;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _ranges.Load(stoppingToken);
|
||||
await cdnRanges.Load(stoppingToken);
|
||||
await Task.Delay(FirstWait, stoppingToken);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (_options.CurrentValue.Cdn.AutoUpdate)
|
||||
if (options.CurrentValue.Cdn.AutoUpdate)
|
||||
await Update(CdnCatalog.All, stoppingToken);
|
||||
await Task.Delay(Interval + TimeSpan.FromMinutes(Random.Shared.Next(60)), stoppingToken);
|
||||
}
|
||||
@@ -51,7 +43,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "{Service} stopped", nameof(CdnUpdater));
|
||||
logger.LogWarning(ex, "{Service} stopped", nameof(CdnUpdater));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,13 +68,13 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
set.Sources = provider.Lists.Select(l => l.Url).ToList();
|
||||
set.FetchedAt = DateTime.UtcNow;
|
||||
await DB.Default.SaveAsync(set, token);
|
||||
_ranges.Replace(set);
|
||||
cdnRanges.Replace(set);
|
||||
replaced++;
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or InvalidDataException or FormatException or System.Text.Json.JsonException
|
||||
or TaskCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogWarning(ex, "Could not update the published ranges of {Cdn}", provider.Name);
|
||||
logger.LogWarning(ex, "Could not update the published ranges of {Cdn}", provider.Name);
|
||||
}
|
||||
}
|
||||
return replaced;
|
||||
@@ -94,14 +86,14 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
request.Headers.Accept.ParseAdd("application/json, text/plain;q=0.9");
|
||||
if (list.Body != default)
|
||||
request.Content = new StringContent(list.Body, Encoding.UTF8, "application/x-www-form-urlencoded");
|
||||
using var response = await _http.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
using var response = await http.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (response.Content.Headers.ContentLength > MaxBytes)
|
||||
throw new InvalidDataException($"more than {MaxBytes} bytes");
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(token);
|
||||
using var buffer = new MemoryStream();
|
||||
var chunk = new byte[81920];
|
||||
int read;
|
||||
var read = default(int);
|
||||
while ((read = await stream.ReadAsync(chunk, token)) > 0)
|
||||
{
|
||||
if (buffer.Length + read > MaxBytes)
|
||||
|
||||
@@ -23,14 +23,14 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
public string Source => default;
|
||||
}
|
||||
|
||||
public sealed class DbIpLocator : IGeoLocator, IDisposable
|
||||
public sealed class DbIpLocator(
|
||||
IOptionsMonitor<StatisticsOptions> options,
|
||||
ILogger<DbIpLocator> logger) : IGeoLocator, IDisposable
|
||||
{
|
||||
public static readonly string CityFile = GeoUpdater.FileOf("city");
|
||||
public static readonly string AsnFile = GeoUpdater.FileOf("asn");
|
||||
static readonly TimeSpan CheckInterval = TimeSpan.FromMinutes(10);
|
||||
|
||||
readonly IOptionsMonitor<StatisticsOptions> _options;
|
||||
readonly ILogger<DbIpLocator> _logger;
|
||||
readonly object _lock = new();
|
||||
Reader _city;
|
||||
Reader _asn;
|
||||
@@ -39,12 +39,6 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
DateTime _checkedAt = DateTime.MinValue;
|
||||
bool _warned;
|
||||
|
||||
public DbIpLocator(IOptionsMonitor<StatisticsOptions> options, ILogger<DbIpLocator> logger)
|
||||
{
|
||||
_options = options;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string Source
|
||||
{
|
||||
get
|
||||
@@ -60,7 +54,8 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
if (address == default || !IpRangeGuard.IsPublic(address))
|
||||
return default;
|
||||
Refresh();
|
||||
Dictionary<string, object> place, network;
|
||||
var place = default(Dictionary<string, object>);
|
||||
var network = default(Dictionary<string, object>);
|
||||
try
|
||||
{
|
||||
// under the lock, so a reload cannot dispose a reader in the middle of a lookup; lookups are rare
|
||||
@@ -82,7 +77,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidDatabaseException or ArgumentException)
|
||||
{
|
||||
_logger.LogWarning(ex, "The geolocation databases could not be read");
|
||||
logger.LogWarning(ex, "The geolocation databases could not be read");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -104,13 +99,13 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
if (DateTime.UtcNow - _checkedAt < CheckInterval)
|
||||
return;
|
||||
_checkedAt = DateTime.UtcNow;
|
||||
var directory = _options.CurrentValue.GeoDirectory;
|
||||
var directory = options.CurrentValue.GeoDirectory;
|
||||
(_city, _cityStamp) = Open(Path.Combine(directory ?? string.Empty, CityFile), _city, _cityStamp);
|
||||
(_asn, _asnStamp) = Open(Path.Combine(directory ?? string.Empty, AsnFile), _asn, _asnStamp);
|
||||
if (_city == default && _asn == default && !_warned)
|
||||
{
|
||||
_warned = true;
|
||||
_logger.LogInformation("No geolocation databases in {Directory}; servers are not located", directory);
|
||||
logger.LogInformation("No geolocation databases in {Directory}; servers are not located", directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,7 +128,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidDatabaseException or IOException)
|
||||
{
|
||||
_logger.LogWarning(ex, "{Path} is not a readable geolocation database", path);
|
||||
logger.LogWarning(ex, "{Path} is not a readable geolocation database", path);
|
||||
return (current, stamp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,25 +13,17 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
// whether each database was built this month and, if not, fetches this month's (or, early in the month, last month's),
|
||||
// checks it opens as the right kind of database and swaps it in. This, CdnUpdater and SMTP are the server's only traffic
|
||||
// that is not federation, which is why each has its own client instead of IFederationHttp.
|
||||
public sealed class GeoUpdater : BackgroundService
|
||||
public sealed class GeoUpdater(
|
||||
IHttpClientFactory http,
|
||||
IOptionsMonitor<StatisticsOptions> statisticsOptions,
|
||||
IGeoLocator geoLocator,
|
||||
ILogger<GeoUpdater> logger) : BackgroundService
|
||||
{
|
||||
public const string ClientName = "geo";
|
||||
static readonly string[] Kinds = { "city", "asn" };
|
||||
static readonly TimeSpan FirstWait = TimeSpan.FromSeconds(30);
|
||||
static readonly TimeSpan Interval = TimeSpan.FromHours(24);
|
||||
|
||||
readonly IHttpClientFactory _http;
|
||||
readonly IOptionsMonitor<StatisticsOptions> _options;
|
||||
readonly IGeoLocator _locator;
|
||||
readonly ILogger<GeoUpdater> _logger;
|
||||
|
||||
public GeoUpdater(IHttpClientFactory http, IOptionsMonitor<StatisticsOptions> options, IGeoLocator locator, ILogger<GeoUpdater> logger)
|
||||
{
|
||||
_http = http;
|
||||
_options = options;
|
||||
_locator = locator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public static string FileOf(string kind) => $"dbip-{kind}-lite.mmdb";
|
||||
|
||||
@@ -42,7 +34,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
await Task.Delay(FirstWait, stoppingToken);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (_options.CurrentValue.Geo.AutoUpdate)
|
||||
if (statisticsOptions.CurrentValue.Geo.AutoUpdate)
|
||||
await Update(DateTime.UtcNow, stoppingToken);
|
||||
await Task.Delay(Interval + TimeSpan.FromMinutes(Random.Shared.Next(60)), stoppingToken);
|
||||
}
|
||||
@@ -55,7 +47,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
// The number of databases replaced. A failure keeps the database already there and is tried again the next day.
|
||||
public async Task<int> Update(DateTime now, CancellationToken token)
|
||||
{
|
||||
var options = _options.CurrentValue;
|
||||
var options = statisticsOptions.CurrentValue;
|
||||
var replaced = 0;
|
||||
try
|
||||
{
|
||||
@@ -63,7 +55,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Cannot create {Directory}; servers are not located", options.GeoDirectory);
|
||||
logger.LogWarning(ex, "Cannot create {Directory}; servers are not located", options.GeoDirectory);
|
||||
return 0;
|
||||
}
|
||||
foreach (var kind in Kinds)
|
||||
@@ -79,19 +71,19 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
if (await Install(kind, month, path, options, token))
|
||||
{
|
||||
replaced++;
|
||||
_logger.LogInformation("Installed DB-IP Lite {Kind} {Month:yyyy-MM}", kind, month);
|
||||
logger.LogInformation("Installed DB-IP Lite {Kind} {Month:yyyy-MM}", kind, month);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or IOException or InvalidDatabaseException or InvalidDataException
|
||||
or TaskCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogWarning(ex, "Could not install DB-IP Lite {Kind} {Month:yyyy-MM}", kind, month);
|
||||
logger.LogWarning(ex, "Could not install DB-IP Lite {Kind} {Month:yyyy-MM}", kind, month);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (replaced > 0 && _locator is DbIpLocator locator)
|
||||
if (replaced > 0 && geoLocator is DbIpLocator locator)
|
||||
locator.Reload();
|
||||
return replaced;
|
||||
}
|
||||
@@ -121,7 +113,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
var unpacked = path + ".new";
|
||||
try
|
||||
{
|
||||
using (var response = await _http.CreateClient(ClientName).GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token))
|
||||
using (var response = await http.CreateClient(ClientName).GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token))
|
||||
{
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
return false;
|
||||
@@ -150,7 +142,7 @@ namespace PrivaPub.Infrastructure.Geo
|
||||
{
|
||||
var buffer = new byte[81920];
|
||||
long total = 0;
|
||||
int read;
|
||||
var read = default(int);
|
||||
while ((read = await source.ReadAsync(buffer, token)) > 0)
|
||||
{
|
||||
total += read;
|
||||
|
||||
Reference in new issue
Block a user