namespace AvParser.Core.Proxies.Selection;
///
/// Holds one proxy until it fails.
///
///
/// The default because it is the only strategy that keeps a site's session coherent: rotating on
/// every request changes the apparent client mid-session, which reliably triggers re-logins and
/// captchas on anything that tracks cookies.
///
public sealed class StickyProxySelection : IProxySelectionStrategy
{
private ProxyEntry? _current;
///
public ProxyRotation Kind => ProxyRotation.Sticky;
///
public ProxyEntry? Pick(IReadOnlyList candidates)
{
ArgumentNullException.ThrowIfNull(candidates);
// Keep the current pick only while it is still among the available candidates: a refresh
// or a quarantine may have taken it out from under us.
if (_current is not null && candidates.Contains(_current))
{
return _current;
}
_current = candidates.Count > 0 ? candidates[0] : null;
return _current;
}
///
public void Report(ProxyEntry entry, bool success)
{
if (!success && ReferenceEquals(entry, _current))
{
_current = null;
}
}
///
public void Reset() => _current = null;
}