| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http |
| | | 9 | | { |
| | | 10 | | internal sealed class HttpConnectionWaiter<T> : TaskCompletionSourceWithCancellation<T> |
| | | 11 | | where T : HttpConnectionBase? |
| | | 12 | | { |
| | | 13 | | // When a connection attempt is pending, reference the connection's CTS, so we can tear it down if the initiatin |
| | | 14 | | // or completes on a different connection. |
| | | 15 | | public CancellationTokenSource? ConnectionCancellationTokenSource; |
| | | 16 | | |
| | | 17 | | // Distinguish connection cancellation that happens because the initiating request is cancelled or completed on |
| | 0 | 18 | | public bool CancelledByOriginatingRequestCompletion { get; set; } |
| | | 19 | | |
| | | 20 | | public ValueTask<T> WaitForConnectionAsync(HttpRequestMessage request, HttpConnectionPool pool, bool async, Canc |
| | 0 | 21 | | { |
| | 0 | 22 | | bool withTelemetry = HttpTelemetry.Log.IsEnabled() |
| | 0 | 23 | | || (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled && pool.Settings._metrics!.Reque |
| | 0 | 24 | | || (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation && Activity.Current? |
| | 0 | 25 | | return withTelemetry |
| | 0 | 26 | | ? WaitForConnectionWithTelemetryAsync(request, pool, async, requestCancellationToken) |
| | 0 | 27 | | : WaitWithCancellationAsync(async, requestCancellationToken); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | private async ValueTask<T> WaitForConnectionWithTelemetryAsync(HttpRequestMessage request, HttpConnectionPool po |
| | 0 | 31 | | { |
| | | 32 | | // The HTTP/3 connection waiting span should include the time spent waiting for an available QUIC stream, th |
| | 0 | 33 | | Debug.Assert(typeof(T) == typeof(HttpConnection) || typeof(T) == typeof(Http2Connection)); |
| | | 34 | | |
| | 0 | 35 | | long startingTimestamp = Stopwatch.GetTimestamp(); |
| | | 36 | | |
| | 0 | 37 | | using Activity? waitForConnectionActivity = ConnectionSetupDistributedTracing.StartWaitForConnectionActivity |
| | | 38 | | try |
| | 0 | 39 | | { |
| | 0 | 40 | | return await WaitWithCancellationAsync(async, requestCancellationToken).ConfigureAwait(false); |
| | | 41 | | } |
| | 0 | 42 | | catch (Exception ex) when (waitForConnectionActivity is not null) |
| | 0 | 43 | | { |
| | 0 | 44 | | ConnectionSetupDistributedTracing.ReportError(waitForConnectionActivity, ex); |
| | 0 | 45 | | throw; |
| | | 46 | | } |
| | | 47 | | finally |
| | 0 | 48 | | { |
| | 0 | 49 | | if (HttpTelemetry.Log.IsEnabled() || GlobalHttpSettings.MetricsHandler.IsGloballyEnabled) |
| | 0 | 50 | | { |
| | 0 | 51 | | TimeSpan duration = Stopwatch.GetElapsedTime(startingTimestamp); |
| | 0 | 52 | | int versionMajor = typeof(T) == typeof(HttpConnection) ? 1 : 2; |
| | 0 | 53 | | if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled) |
| | 0 | 54 | | { |
| | 0 | 55 | | pool.Settings._metrics!.RequestLeftQueue(request, pool, duration, versionMajor); |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if (HttpTelemetry.Log.IsEnabled()) |
| | 0 | 59 | | { |
| | 0 | 60 | | HttpTelemetry.Log.RequestLeftQueue(versionMajor, duration); |
| | 0 | 61 | | } |
| | 0 | 62 | | } |
| | 0 | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public bool TrySignal(T connection) |
| | 0 | 67 | | { |
| | 0 | 68 | | Debug.Assert(connection is not null); |
| | | 69 | | |
| | 0 | 70 | | if (TrySetResult(connection)) |
| | 0 | 71 | | { |
| | 0 | 72 | | if (NetEventSource.Log.IsEnabled()) connection.Trace("Dequeued waiting request."); |
| | 0 | 73 | | return true; |
| | | 74 | | } |
| | | 75 | | else |
| | 0 | 76 | | { |
| | 0 | 77 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 78 | | { |
| | 0 | 79 | | connection.Trace(Task.IsCanceled |
| | 0 | 80 | | ? "Discarding canceled request from queue." |
| | 0 | 81 | | : "Discarding signaled request waiter from queue."); |
| | 0 | 82 | | } |
| | 0 | 83 | | return false; |
| | | 84 | | } |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public void SetTimeoutToPendingConnectionAttempt(HttpConnectionPool pool, bool requestCancelled) |
| | 0 | 88 | | { |
| | 0 | 89 | | int timeout = GlobalHttpSettings.SocketsHttpHandler.PendingConnectionTimeoutOnRequestCompletion; |
| | 0 | 90 | | if (ConnectionCancellationTokenSource is null || |
| | 0 | 91 | | timeout == Timeout.Infinite || |
| | 0 | 92 | | pool.Settings._connectTimeout != Timeout.InfiniteTimeSpan && timeout > (int)pool.Settings._connectTimeou |
| | 0 | 93 | | { |
| | 0 | 94 | | return; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | lock (this) |
| | 0 | 98 | | { |
| | 0 | 99 | | if (ConnectionCancellationTokenSource is null) |
| | 0 | 100 | | { |
| | 0 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 105 | | { |
| | 0 | 106 | | pool.Trace($"Initiating cancellation of a pending connection attempt with delay of {timeout} ms, " + |
| | 0 | 107 | | $"Reason: {(requestCancelled ? "Request cancelled" : "Request served by another connection")}.") |
| | 0 | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | CancelledByOriginatingRequestCompletion = true; |
| | 0 | 111 | | if (timeout > 0) |
| | 0 | 112 | | { |
| | | 113 | | // Cancel after the specified timeout. This cancellation will not fire if the connection |
| | | 114 | | // succeeds within the delay and the CTS becomes disposed. |
| | 0 | 115 | | ConnectionCancellationTokenSource.CancelAfter(timeout); |
| | 0 | 116 | | } |
| | | 117 | | else |
| | 0 | 118 | | { |
| | | 119 | | // Cancel immediately if no timeout specified. |
| | 0 | 120 | | ConnectionCancellationTokenSource.Cancel(); |
| | 0 | 121 | | } |
| | 0 | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |