| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Dispatcher |
| | | 10 | | { |
| | | 11 | | internal sealed class QuotaThrottle |
| | | 12 | | { |
| | | 13 | | private readonly object _mutex; |
| | | 14 | | private readonly Queue<TaskCompletionSource<object>> _waiters; |
| | | 15 | | //private bool _didTraceThrottleLimit; |
| | | 16 | | // private string _propertyName = "ManualFlowControlLimit"; // Used for eventing |
| | | 17 | | #pragma warning disable IDE0052 // Remove unread private members |
| | | 18 | | private string _owner; // Used for eventing |
| | | 19 | | #pragma warning restore IDE0052 // Remove unread private members |
| | | 20 | | |
| | 0 | 21 | | internal QuotaThrottle(object mutex) |
| | | 22 | | { |
| | 0 | 23 | | Limit = int.MaxValue; |
| | 0 | 24 | | _mutex = mutex; |
| | 0 | 25 | | _waiters = new Queue<TaskCompletionSource<object>>(); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | private bool IsEnabled |
| | | 29 | | { |
| | 0 | 30 | | get { return Limit != int.MaxValue; } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | internal string Owner |
| | | 34 | | { |
| | 0 | 35 | | set { _owner = value; } |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | internal int Limit { get; private set; } |
| | | 39 | | |
| | | 40 | | internal Task AcquireAsync() |
| | | 41 | | { |
| | 0 | 42 | | lock (_mutex) |
| | | 43 | | { |
| | 0 | 44 | | if (IsEnabled) |
| | | 45 | | { |
| | 0 | 46 | | if (Limit > 0) |
| | | 47 | | { |
| | 0 | 48 | | Limit--; |
| | | 49 | | |
| | 0 | 50 | | if (Limit == 0) |
| | | 51 | | { |
| | | 52 | | // TODO: Events |
| | | 53 | | //if (DiagnosticUtility.ShouldTraceWarning && !_didTraceThrottleLimit) |
| | | 54 | | //{ |
| | | 55 | | // _didTraceThrottleLimit = true; |
| | | 56 | | |
| | | 57 | | // TraceUtility.TraceEvent( |
| | | 58 | | // TraceEventType.Warning, |
| | | 59 | | // TraceCode.ManualFlowThrottleLimitReached, |
| | | 60 | | // SR.GetString(SR.TraceCodeManualFlowThrottleLimitReached, |
| | | 61 | | // _propertyName, _owner)); |
| | | 62 | | //} |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return Task.CompletedTask; |
| | | 66 | | } |
| | | 67 | | else |
| | | 68 | | { |
| | 0 | 69 | | var tcs = new TaskCompletionSource<object>(); |
| | 0 | 70 | | _waiters.Enqueue(tcs); |
| | 0 | 71 | | return tcs.Task; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | else |
| | | 75 | | { |
| | 0 | 76 | | return Task.CompletedTask; |
| | | 77 | | } |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | internal int IncrementLimit(int incrementBy) |
| | | 82 | | { |
| | 0 | 83 | | if (incrementBy < 0) |
| | | 84 | | { |
| | 0 | 85 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(increme |
| | 0 | 86 | | SRCommon.ValueMustBeNonNegative)); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | int newLimit; |
| | 0 | 90 | | TaskCompletionSource<object>[] released = null; |
| | | 91 | | |
| | 0 | 92 | | lock (_mutex) |
| | | 93 | | { |
| | 0 | 94 | | if (IsEnabled) |
| | | 95 | | { |
| | 0 | 96 | | checked { Limit += incrementBy; } |
| | 0 | 97 | | released = LimitChanged(); |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | newLimit = Limit; |
| | 0 | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | if (released != null) |
| | | 104 | | { |
| | 0 | 105 | | Release(released); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return newLimit; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private TaskCompletionSource<object>[] LimitChanged() |
| | | 112 | | { |
| | 0 | 113 | | TaskCompletionSource<object>[] released = null; |
| | | 114 | | |
| | 0 | 115 | | if (IsEnabled) |
| | | 116 | | { |
| | 0 | 117 | | if ((_waiters.Count > 0) && (Limit > 0)) |
| | | 118 | | { |
| | 0 | 119 | | if (Limit < _waiters.Count) |
| | | 120 | | { |
| | 0 | 121 | | released = new TaskCompletionSource<object>[Limit]; |
| | 0 | 122 | | for (int i = 0; i < Limit; i++) |
| | | 123 | | { |
| | 0 | 124 | | released[i] = _waiters.Dequeue(); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | Limit = 0; |
| | | 128 | | } |
| | | 129 | | else |
| | | 130 | | { |
| | 0 | 131 | | released = _waiters.ToArray(); |
| | 0 | 132 | | _waiters.Clear(); |
| | 0 | 133 | | _waiters.TrimExcess(); |
| | | 134 | | |
| | 0 | 135 | | Limit -= released.Length; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | //didTraceThrottleLimit = false; |
| | | 139 | | } |
| | | 140 | | else |
| | | 141 | | { |
| | 0 | 142 | | released = _waiters.ToArray(); |
| | 0 | 143 | | _waiters.Clear(); |
| | 0 | 144 | | _waiters.TrimExcess(); |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | return released; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | internal void SetLimit(int messageLimit) |
| | | 151 | | { |
| | 0 | 152 | | if (messageLimit < 0) |
| | | 153 | | { |
| | 0 | 154 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(message |
| | 0 | 155 | | SRCommon.ValueMustBeNonNegative)); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | TaskCompletionSource<object>[] released = null; |
| | | 159 | | |
| | 0 | 160 | | lock (_mutex) |
| | | 161 | | { |
| | 0 | 162 | | Limit = messageLimit; |
| | 0 | 163 | | released = LimitChanged(); |
| | 0 | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | if (released != null) |
| | | 167 | | { |
| | 0 | 168 | | Release(released); |
| | | 169 | | } |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | private void ReleaseAsync(object state) |
| | | 173 | | { |
| | 0 | 174 | | ((TaskCompletionSource<object>)state).TrySetResult(null); |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | internal void Release(TaskCompletionSource<object>[] released) |
| | | 178 | | { |
| | 0 | 179 | | for (int i = 0; i < released.Length; i++) |
| | | 180 | | { |
| | 0 | 181 | | ActionItem.Schedule(ReleaseAsync, released[i]); |
| | | 182 | | } |
| | 0 | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |