Windows 0.3.0 clean next-generation source delta; no private history or other platform changes
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
namespace UCVL.VPNPlus;
|
||||
|
||||
// Advisory only: detects an available authorized alternate, never starts/stops a
|
||||
// core, changes routes, fetches grants, or turns a TCP result into proxy proof.
|
||||
// The caller owns the snapshot and performs one read-only probe per ticket.
|
||||
internal sealed class TailnetReadinessPolicy
|
||||
{
|
||||
internal readonly record struct Context(long Epoch, int? CoreId, bool Wanted,
|
||||
bool Busy, bool Running, bool UsesTailnet, bool HasAuthorizedCandidate);
|
||||
internal readonly record struct ProbeTicket(long Generation, long Sequence, long Epoch, int CoreId);
|
||||
internal enum Result { Ignore, NotReady, Confirming, BecameReady, Ready }
|
||||
|
||||
internal const long IntervalMilliseconds = 30_000;
|
||||
internal const long MaximumBackoffMilliseconds = 120_000;
|
||||
private bool _bound, _inFlight;
|
||||
private long _generation, _sequence, _epoch, _nextDue;
|
||||
private int? _core;
|
||||
private int _successes, _failures;
|
||||
internal bool IsReady { get; private set; }
|
||||
internal long NextDue => _nextDue;
|
||||
|
||||
internal void Reset()
|
||||
{
|
||||
_generation++;
|
||||
_bound = _inFlight = IsReady = false;
|
||||
_core = null;
|
||||
_nextDue = 0;
|
||||
_successes = _failures = 0;
|
||||
}
|
||||
|
||||
private static bool Eligible(Context context) => context.Wanted && context.Running
|
||||
&& context.CoreId is not null && !context.UsesTailnet && context.HasAuthorizedCandidate;
|
||||
|
||||
private void Synchronize(Context context)
|
||||
{
|
||||
if (!_bound || _epoch != context.Epoch || _core != context.CoreId)
|
||||
{
|
||||
Reset();
|
||||
_bound = true;
|
||||
_epoch = context.Epoch;
|
||||
_core = context.CoreId;
|
||||
}
|
||||
if (!Eligible(context) && (_inFlight || IsReady || _successes != 0 || _failures != 0))
|
||||
{
|
||||
// A grant withdrawal/disconnect must invalidate the outstanding
|
||||
// ticket even when the process ID has not changed yet.
|
||||
_generation++;
|
||||
_inFlight = IsReady = false;
|
||||
_nextDue = 0;
|
||||
_successes = _failures = 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal bool TryBegin(Context context, long now, out ProbeTicket ticket)
|
||||
{
|
||||
ticket = default;
|
||||
Synchronize(context);
|
||||
if (!Eligible(context) || context.Busy || _inFlight || now < _nextDue) return false;
|
||||
_inFlight = true;
|
||||
ticket = new(_generation, ++_sequence, context.Epoch, context.CoreId!.Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
internal Result Observe(ProbeTicket ticket, Context context, long now, bool reachable)
|
||||
{
|
||||
Synchronize(context);
|
||||
if (!_inFlight || ticket.Generation != _generation || ticket.Sequence != _sequence
|
||||
|| ticket.Epoch != context.Epoch || ticket.CoreId != context.CoreId || !Eligible(context))
|
||||
return Result.Ignore;
|
||||
_inFlight = false;
|
||||
_nextDue = now + IntervalMilliseconds;
|
||||
// Do not publish a result obtained while connection/authorization state
|
||||
// is being changed. A new ticket can retry after the normal cadence.
|
||||
if (context.Busy) return Result.Ignore;
|
||||
if (!reachable)
|
||||
{
|
||||
IsReady = false;
|
||||
_successes = 0;
|
||||
_failures = Math.Min(_failures + 1, 3);
|
||||
_nextDue = now + Math.Min(MaximumBackoffMilliseconds,
|
||||
IntervalMilliseconds * (1L << (_failures - 1)));
|
||||
return Result.NotReady;
|
||||
}
|
||||
_failures = 0;
|
||||
_successes = Math.Min(_successes + 1, 2);
|
||||
if (_successes < 2) return Result.Confirming;
|
||||
if (IsReady) return Result.Ready;
|
||||
IsReady = true;
|
||||
return Result.BecameReady;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user