33 lines
1.5 KiB
C#
33 lines
1.5 KiB
C#
namespace UCVL.VPNPlus;
|
|
|
|
// Give an authorized port a full TCP probe budget. Quick failures may retry
|
|
// within the same round. The readiness policy schedules a confirmation round.
|
|
internal static class TailnetEndpointConfirmation
|
|
{
|
|
private const int Attempts = 3;
|
|
internal static async Task<bool> ProbeAsync(Func<CancellationToken, Task<bool>> probe,
|
|
CancellationToken token, TimeSpan? budget = null)
|
|
{
|
|
token.ThrowIfCancellationRequested();
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
|
|
deadline.CancelAfter(budget ?? TimeSpan.FromSeconds(TailnetTransport.EndpointProbeTimeoutSeconds));
|
|
for (var attempt = 0; attempt < Attempts; attempt++)
|
|
{
|
|
token.ThrowIfCancellationRequested();
|
|
if (deadline.IsCancellationRequested) return false;
|
|
try
|
|
{
|
|
var reachable = await probe(deadline.Token).WaitAsync(deadline.Token).ConfigureAwait(false);
|
|
token.ThrowIfCancellationRequested();
|
|
if (deadline.IsCancellationRequested) return false;
|
|
if (reachable) return true;
|
|
}
|
|
catch (OperationCanceledException) when (!token.IsCancellationRequested && deadline.IsCancellationRequested) { return false; }
|
|
catch (OperationCanceledException) { throw; }
|
|
catch (Exception) { /* One failed TCP attempt is inconclusive. */ }
|
|
}
|
|
token.ThrowIfCancellationRequested();
|
|
return false;
|
|
}
|
|
}
|