Skip to content

Commit

Permalink
pr comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Jan 17, 2024
1 parent 0bf0c33 commit 03e57c2
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions src/NATS.Client.Core/Internal/SslStreamConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,12 @@ public async Task AuthenticateAsClientAsync(NatsUri uri, TimeSpan timeout)
var options = SslClientAuthenticationOptions(uri);
try
{
using var ctsTimer = new CancellationTokenSource();
using var ctsAuth = new CancellationTokenSource();

var taskSsl = _sslStream.AuthenticateAsClientAsync(options, ctsAuth.Token);
var taskTimeout = Task.Delay(timeout, ctsTimer.Token);

var task = await Task.WhenAny(taskSsl, taskTimeout).ConfigureAwait(false);
if (task == taskTimeout)
{
#if NET6_0
ctsAuth.Cancel();
#else
await ctsAuth.CancelAsync().ConfigureAwait(false);
#endif
throw new NatsException("TLS authentication timed out");
}

#if NET6_0
ctsTimer.Cancel();
#else
await ctsTimer.CancelAsync().ConfigureAwait(false);
#endif
await taskSsl.ConfigureAwait(true);
using var cts = new CancellationTokenSource(timeout);
await _sslStream.AuthenticateAsClientAsync(options, cts.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw new NatsException("TLS authentication timed out");
}
catch (AuthenticationException ex)
{
Expand Down

0 comments on commit 03e57c2

Please sign in to comment.