| | | 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.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http |
| | | 8 | | { |
| | | 9 | | internal struct RequestQueue<T> |
| | | 10 | | where T : HttpConnectionBase? |
| | | 11 | | { |
| | | 12 | | public struct QueueItem |
| | | 13 | | { |
| | | 14 | | public HttpRequestMessage Request; |
| | | 15 | | public HttpConnectionWaiter<T> Waiter; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | // This implementation mimics that of Queue<T>, but without version checks and with an extra head pointer |
| | | 19 | | // https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Collections/Gener |
| | | 20 | | private QueueItem[] _array; |
| | | 21 | | private int _head; // The index from which to dequeue if the queue isn't empty. |
| | | 22 | | private int _tail; // The index at which to enqueue if the queue isn't full. |
| | | 23 | | private int _size; // Number of elements. |
| | | 24 | | private int _attemptedConnectionsOffset; // The offset from head where we should next peek for a request without |
| | | 25 | | |
| | | 26 | | public RequestQueue() |
| | 0 | 27 | | { |
| | 0 | 28 | | _array = Array.Empty<QueueItem>(); |
| | 0 | 29 | | _head = 0; |
| | 0 | 30 | | _tail = 0; |
| | 0 | 31 | | _size = 0; |
| | 0 | 32 | | _attemptedConnectionsOffset = 0; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | private void Enqueue(QueueItem queueItem) |
| | 0 | 36 | | { |
| | 0 | 37 | | if (_size == _array.Length) |
| | 0 | 38 | | { |
| | 0 | 39 | | Grow(); |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | _array[_tail] = queueItem; |
| | 0 | 43 | | MoveNext(ref _tail); |
| | | 44 | | |
| | 0 | 45 | | _size++; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | private QueueItem Dequeue() |
| | 0 | 49 | | { |
| | 0 | 50 | | Debug.Assert(_size > 0); |
| | | 51 | | |
| | 0 | 52 | | int head = _head; |
| | 0 | 53 | | QueueItem[] array = _array; |
| | | 54 | | |
| | 0 | 55 | | QueueItem queueItem = array[head]; |
| | 0 | 56 | | array[head] = default; |
| | | 57 | | |
| | 0 | 58 | | MoveNext(ref _head); |
| | | 59 | | |
| | 0 | 60 | | if (_attemptedConnectionsOffset > 0) |
| | 0 | 61 | | { |
| | 0 | 62 | | _attemptedConnectionsOffset--; |
| | 0 | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | _size--; |
| | 0 | 66 | | return queueItem; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private bool TryPeek(out QueueItem queueItem) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (_size == 0) |
| | 0 | 72 | | { |
| | 0 | 73 | | queueItem = default!; |
| | 0 | 74 | | return false; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | queueItem = _array[_head]; |
| | 0 | 78 | | return true; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | private void MoveNext(ref int index) |
| | 0 | 82 | | { |
| | 0 | 83 | | int tmp = index + 1; |
| | 0 | 84 | | if (tmp == _array.Length) |
| | 0 | 85 | | { |
| | 0 | 86 | | tmp = 0; |
| | 0 | 87 | | } |
| | 0 | 88 | | index = tmp; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | private void Grow() |
| | 0 | 92 | | { |
| | 0 | 93 | | var newArray = new QueueItem[Math.Max(4, _array.Length * 2)]; |
| | | 94 | | |
| | 0 | 95 | | if (_size != 0) |
| | 0 | 96 | | { |
| | 0 | 97 | | if (_head < _tail) |
| | 0 | 98 | | { |
| | 0 | 99 | | Array.Copy(_array, _head, newArray, 0, _size); |
| | 0 | 100 | | } |
| | | 101 | | else |
| | 0 | 102 | | { |
| | 0 | 103 | | Array.Copy(_array, _head, newArray, 0, _array.Length - _head); |
| | 0 | 104 | | Array.Copy(_array, 0, newArray, _array.Length - _head, _tail); |
| | 0 | 105 | | } |
| | 0 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | _array = newArray; |
| | 0 | 109 | | _head = 0; |
| | 0 | 110 | | _tail = _size; |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | |
| | | 114 | | public HttpConnectionWaiter<T> EnqueueRequest(HttpRequestMessage request) |
| | 0 | 115 | | { |
| | 0 | 116 | | var waiter = new HttpConnectionWaiter<T>(); |
| | 0 | 117 | | EnqueueRequest(request, waiter); |
| | 0 | 118 | | return waiter; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | |
| | | 122 | | public void EnqueueRequest(HttpRequestMessage request, HttpConnectionWaiter<T> waiter) |
| | 0 | 123 | | { |
| | 0 | 124 | | Enqueue(new QueueItem { Request = request, Waiter = waiter }); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | public void PruneCompletedRequestsFromHeadOfQueue(HttpConnectionPool pool) |
| | 0 | 128 | | { |
| | 0 | 129 | | while (TryPeek(out QueueItem queueItem) && queueItem.Waiter.Task.IsCompleted) |
| | 0 | 130 | | { |
| | 0 | 131 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 132 | | { |
| | 0 | 133 | | pool.Trace(queueItem.Waiter.Task.IsCanceled |
| | 0 | 134 | | ? "Discarding canceled request from queue." |
| | 0 | 135 | | : "Discarding signaled request waiter from queue."); |
| | 0 | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | Dequeue(); |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | public bool TryDequeueWaiter(HttpConnectionPool pool, [MaybeNullWhen(false)] out HttpConnectionWaiter<T> waiter) |
| | 0 | 143 | | { |
| | 0 | 144 | | PruneCompletedRequestsFromHeadOfQueue(pool); |
| | | 145 | | |
| | 0 | 146 | | if (Count != 0) |
| | 0 | 147 | | { |
| | 0 | 148 | | waiter = Dequeue().Waiter; |
| | 0 | 149 | | return true; |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | waiter = null; |
| | 0 | 153 | | return false; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | public void TryDequeueSpecificWaiter(HttpConnectionWaiter<T> waiter) |
| | 0 | 157 | | { |
| | 0 | 158 | | if (TryPeek(out QueueItem queueItem) && queueItem.Waiter == waiter) |
| | 0 | 159 | | { |
| | 0 | 160 | | Dequeue(); |
| | 0 | 161 | | } |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | public QueueItem PeekNextRequestForConnectionAttempt() |
| | 0 | 165 | | { |
| | 0 | 166 | | Debug.Assert(_attemptedConnectionsOffset >= 0); |
| | 0 | 167 | | Debug.Assert(_attemptedConnectionsOffset < _size, $"{_attemptedConnectionsOffset} < {_size}"); |
| | | 168 | | |
| | 0 | 169 | | int index = _head + _attemptedConnectionsOffset; |
| | 0 | 170 | | _attemptedConnectionsOffset++; |
| | | 171 | | |
| | 0 | 172 | | if (index >= _array.Length) |
| | 0 | 173 | | { |
| | 0 | 174 | | index -= _array.Length; |
| | 0 | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return _array[index]; |
| | 0 | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | public int Count => _size; |
| | | 181 | | |
| | 0 | 182 | | public int RequestsWithoutAConnectionAttempt => _size - _attemptedConnectionsOffset; |
| | | 183 | | } |
| | | 184 | | } |