Load the proxy pool at startup, and stop old settings files zeroing new defaults

The pool was empty until someone pressed Refresh, which also meant the parser
would have had nothing to work with. A ProxyPoolLoader now fills it once at
launch; startup does not await it, because blocking on a public list being
reachable would be the wrong trade, and it never throws. The proxy page joins
the same operation rather than starting a second download, so it reports the
outcome whether it is opened during the load or long after.

That change surfaced a worse bug underneath. The first run still loaded zero
entries with no error logged at all, which turned out to be the feed source
never being asked: options said UseFeed=False and Protocols=None. Neither is
reachable from the UI — both are default(T).

The cause is that AppSettings kept its defaults on property initialisers, and
the source-generated deserialiser does not run them. Reflection-based
deserialisation of "{}" keeps them; the generated context does not. So a
settings.json written before a setting existed came back with default(T) for it:
the proxy feed switched off, the protocol filter empty, the probe timeout zero
and the probe URL blank — and the app looked like the network had failed.

Defaults now live on primary constructor parameters, which STJ applies for
absent JSON members on both paths, so an older file upgrades cleanly. The
regression test writes a settings file from before the proxy settings existed
and asserts each one comes back at its default. ToProxyOptions also treats an
empty protocol filter as "all", since a hand-edited file that matches nothing is
the least useful possible reading of it.

Also quietens IHttpClientFactory to Warning: four Information lines per request
buried everything the app said, and a proxy sweep makes thousands of them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Leonid Pershin
2026-08-13 18:33:41 +03:00
co-authored by Claude Opus 5
parent 8b552470b7
commit 85656e70b0
11 changed files with 432 additions and 48 deletions
@@ -0,0 +1,102 @@
using AvParser.Core.Proxies;
using AvParser.Infrastructure.Proxies;
using Microsoft.Extensions.Logging.Abstractions;
namespace AvParser.Infrastructure.Tests;
public class ProxyPoolLoaderTests
{
private static ProxyPoolLoader Build(out CountingSource source, out ProxyPool pool)
{
source = new CountingSource();
pool = new ProxyPool([source], new NeverProbe(), new ProxyOptions());
return new ProxyPoolLoader(pool, NullLogger<ProxyPoolLoader>.Instance);
}
[Fact]
public async Task Loading_fills_the_pool()
{
var loader = Build(out _, out var pool);
(await loader.EnsureLoadedAsync()).ShouldBe(2);
pool.Entries.Count.ShouldBe(2);
loader.IsLoaded.ShouldBeTrue();
}
[Fact]
public async Task The_feed_is_fetched_once_however_many_callers_there_are()
{
var loader = Build(out var source, out _);
// Startup and the proxy page both ask; a second download of a 600 KB list would be waste.
await Task.WhenAll(loader.EnsureLoadedAsync(), loader.EnsureLoadedAsync(), loader.EnsureLoadedAsync());
await loader.EnsureLoadedAsync();
source.Calls.ShouldBe(1);
}
[Fact]
public async Task Concurrent_callers_share_one_operation()
{
var loader = Build(out _, out _);
var first = loader.EnsureLoadedAsync();
var second = loader.EnsureLoadedAsync();
first.ShouldBeSameAs(second);
await first;
}
[Fact]
public async Task A_source_that_throws_does_not_take_startup_down()
{
var pool = new ProxyPool([new ThrowingSource()], new NeverProbe(), new ProxyOptions());
var loader = new ProxyPoolLoader(pool, NullLogger<ProxyPoolLoader>.Instance);
// The app has to start whether or not a public list is reachable.
(await loader.EnsureLoadedAsync()).ShouldBe(0);
}
private sealed class CountingSource : IProxySource
{
public int Calls { get; private set; }
public string Id => "counting";
public string DisplayName => "Counting source";
public ProxySourceKind Kind => ProxySourceKind.Feed;
public Task<IReadOnlyList<ProxyEndpoint>> GetProxiesAsync(CancellationToken cancellationToken = default)
{
Calls++;
return Task.FromResult<IReadOnlyList<ProxyEndpoint>>([
new ProxyEndpoint(ProxyProtocol.Http, "1.2.3.4", 8080),
new ProxyEndpoint(ProxyProtocol.Socks5, "5.6.7.8", 1080),
]);
}
}
private sealed class ThrowingSource : IProxySource
{
public string Id => "throwing";
public string DisplayName => "Throwing source";
public ProxySourceKind Kind => ProxySourceKind.Feed;
public Task<IReadOnlyList<ProxyEndpoint>> GetProxiesAsync(CancellationToken cancellationToken = default) =>
throw new HttpRequestException("upstream is down");
}
private sealed class NeverProbe : IProxyProbe
{
public Task<ProxyProbeResult> ProbeAsync(
ProxyEndpoint endpoint,
ProxyOptions options,
CancellationToken cancellationToken = default
) => Task.FromResult(ProxyProbeResult.Failure("not used"));
}
}