Files
ucvl-zonghengjia-open-source/windows/ConnectionHealthPolicy.cs

51 lines
2.5 KiB
C#

namespace UCVL.VPNPlus;
// Advisory after connection only; never an Internet-site gate in StartAsync.
// A delayed probe must not undo a user's disconnect or judge a different core.
internal sealed class ConnectionHealthPolicy
{
internal enum Decision { Ignore, Keep, Repair, Degraded, Switch }
internal long Revision { get; private set; }
internal int Failures { get; private set; }
internal bool HasBeenHealthy { get; private set; }
private long _nextProbe;
internal void Invalidate() => Revision++;
internal void BeginCore(long now)
{
Invalidate(); Failures = 0; HasBeenHealthy = false;
_nextProbe = now + 5_000;
}
internal void ResetFailures() => Failures = 0;
internal bool IsDue(long now, bool running) => !running || now >= _nextProbe;
internal Decision Observe(long revision, int? testedCore, int? currentCore,
bool wanted, bool busy, bool transitioning, bool exiting, bool healthy, long now, bool proxyHealthy = false,
bool activeTailnet = false, bool businessUnavailable = false)
{
if (revision != Revision || testedCore != currentCore || !wanted || busy || transitioning || exiting)
return Decision.Ignore;
if (healthy && currentCore is not null) {
HasBeenHealthy = true; Failures = 0; _nextProbe = now + 20_000;
return Decision.Keep;
}
if (currentCore is not null && (businessUnavailable && proxyHealthy || activeTailnet && !proxyHealthy)) {
// Third-party HTTP diagnostics cannot identify an active Tailnet
// transport failure. The bounded periodic ICMP policy owns fallback.
Failures = 0; _nextProbe = now + 20_000;
return Decision.Degraded;
}
if (proxyHealthy && currentCore is not null) {
// Do not punish/restart a working node for a local smart-rule fault.
// The UI must show degraded routing, not declare the business path healthy.
Failures = 0; _nextProbe = now + 20_000;
return Decision.Repair;
}
Failures++;
_nextProbe = now + (HasBeenHealthy ? 20_000 : 5_000);
// Established connections retain tolerance for two transient rounds.
// An unverified newly selected node gets one retry, not a minute of
// false connected state; a dead core does not wait for HTTP timeouts.
return currentCore is null || Failures >= (HasBeenHealthy ? 3 : 2)
? Decision.Switch : Decision.Keep;
}
}