From 03e57c23b8288228318cd7e425e7e73a58537e50 Mon Sep 17 00:00:00 2001 From: Ziya Suzen Date: Wed, 17 Jan 2024 20:48:25 +0000 Subject: [PATCH] pr comment --- .../Internal/SslStreamConnection.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/NATS.Client.Core/Internal/SslStreamConnection.cs b/src/NATS.Client.Core/Internal/SslStreamConnection.cs index d2bf89a3d..46b9b2fe1 100644 --- a/src/NATS.Client.Core/Internal/SslStreamConnection.cs +++ b/src/NATS.Client.Core/Internal/SslStreamConnection.cs @@ -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) {