| | | 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 CreditManager |
| | | 11 | | { |
| | | 12 | | private readonly IHttpTrace _owner; |
| | | 13 | | private readonly string _name; |
| | | 14 | | private int _current; |
| | | 15 | | private bool _disposed; |
| | | 16 | | /// <summary>Circular singly-linked list of active waiters.</summary> |
| | | 17 | | /// <remarks>If null, the list is empty. If non-null, this is the tail. If the list has one item, its Next is |
| | | 18 | | private CreditWaiter? _waitersTail; |
| | | 19 | | |
| | 0 | 20 | | public CreditManager(IHttpTrace owner, string name, int initialCredit) |
| | 0 | 21 | | { |
| | 0 | 22 | | Debug.Assert(owner != null); |
| | 0 | 23 | | Debug.Assert(!string.IsNullOrWhiteSpace(name)); |
| | | 24 | | |
| | 0 | 25 | | if (NetEventSource.Log.IsEnabled()) owner.Trace($"{name}. {nameof(initialCredit)}={initialCredit}"); |
| | 0 | 26 | | _owner = owner; |
| | 0 | 27 | | _name = name; |
| | 0 | 28 | | _current = initialCredit; |
| | 0 | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | public bool IsCreditAvailable => Volatile.Read(ref _current) > 0; |
| | | 32 | | |
| | | 33 | | private object SyncObject |
| | | 34 | | { |
| | | 35 | | // Generally locking on "this" is considered poor form, but this type is internal, |
| | | 36 | | // and it's unnecessary overhead to allocate another object just for this purpose. |
| | 0 | 37 | | get => this; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public bool TryRequestCreditNoWait(int amount) |
| | | 41 | | { |
| | | 42 | | lock (SyncObject) |
| | | 43 | | { |
| | | 44 | | return TryRequestCreditNoLock(amount) > 0; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public ValueTask<int> RequestCreditAsync(int amount, CancellationToken cancellationToken) |
| | 0 | 49 | | { |
| | 0 | 50 | | lock (SyncObject) |
| | 0 | 51 | | { |
| | | 52 | | // If we can satisfy the request with credit already available, do so synchronously. |
| | 0 | 53 | | int granted = TryRequestCreditNoLock(amount); |
| | | 54 | | |
| | 0 | 55 | | if (granted > 0) |
| | 0 | 56 | | { |
| | 0 | 57 | | return new ValueTask<int>(granted); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | if (NetEventSource.Log.IsEnabled()) _owner.Trace($"{_name}. requested={amount}, no credit available."); |
| | | 61 | | |
| | | 62 | | // Otherwise, create a new waiter. |
| | 0 | 63 | | var waiter = new CreditWaiter(cancellationToken); |
| | 0 | 64 | | waiter.Amount = amount; |
| | | 65 | | |
| | | 66 | | // Add the waiter at the tail of the queue. |
| | 0 | 67 | | if (_waitersTail is null) |
| | 0 | 68 | | { |
| | 0 | 69 | | _waitersTail = waiter.Next = waiter; |
| | 0 | 70 | | } |
| | | 71 | | else |
| | 0 | 72 | | { |
| | 0 | 73 | | waiter.Next = _waitersTail.Next; |
| | 0 | 74 | | _waitersTail.Next = waiter; |
| | 0 | 75 | | _waitersTail = waiter; |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | // And return a ValueTask<int> for it. |
| | 0 | 79 | | return waiter.AsValueTask(); |
| | | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | public void AdjustCredit(int amount) |
| | 0 | 84 | | { |
| | | 85 | | // Note credit can be adjusted *downward* as well. |
| | | 86 | | // This can cause the current credit to become negative. |
| | | 87 | | |
| | 0 | 88 | | lock (SyncObject) |
| | 0 | 89 | | { |
| | 0 | 90 | | if (NetEventSource.Log.IsEnabled()) _owner.Trace($"{_name}. {nameof(amount)}={amount}, current={_current |
| | | 91 | | |
| | 0 | 92 | | if (_disposed) |
| | 0 | 93 | | { |
| | 0 | 94 | | return; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | Debug.Assert(_current <= 0 || _waitersTail is null, "Shouldn't have waiters when credit is available"); |
| | | 98 | | |
| | 0 | 99 | | _current = checked(_current + amount); |
| | | 100 | | |
| | 0 | 101 | | while (_current > 0 && _waitersTail != null) |
| | 0 | 102 | | { |
| | | 103 | | // Get the waiter from the head of the queue. |
| | 0 | 104 | | CreditWaiter? waiter = _waitersTail.Next; |
| | 0 | 105 | | Debug.Assert(waiter != null); |
| | 0 | 106 | | int granted = Math.Min(waiter.Amount, _current); |
| | | 107 | | |
| | | 108 | | // Remove the waiter from the list. |
| | 0 | 109 | | if (waiter.Next == waiter) |
| | 0 | 110 | | { |
| | 0 | 111 | | Debug.Assert(_waitersTail == waiter); |
| | 0 | 112 | | _waitersTail = null; |
| | 0 | 113 | | } |
| | | 114 | | else |
| | 0 | 115 | | { |
| | 0 | 116 | | _waitersTail.Next = waiter.Next; |
| | 0 | 117 | | } |
| | 0 | 118 | | waiter.Next = null; |
| | | 119 | | |
| | | 120 | | // Ensure that we grant credit only if the task has not been canceled. |
| | 0 | 121 | | if (waiter.TrySetResult(granted)) |
| | 0 | 122 | | { |
| | 0 | 123 | | _current -= granted; |
| | 0 | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | waiter.Dispose(); |
| | 0 | 127 | | } |
| | 0 | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | public void Dispose() |
| | 0 | 132 | | { |
| | 0 | 133 | | lock (SyncObject) |
| | 0 | 134 | | { |
| | 0 | 135 | | if (_disposed) |
| | 0 | 136 | | { |
| | 0 | 137 | | return; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | _disposed = true; |
| | | 141 | | |
| | 0 | 142 | | CreditWaiter? waiter = _waitersTail; |
| | 0 | 143 | | if (waiter != null) |
| | 0 | 144 | | { |
| | | 145 | | do |
| | 0 | 146 | | { |
| | 0 | 147 | | CreditWaiter? next = waiter!.Next; |
| | 0 | 148 | | waiter.Next = null; |
| | 0 | 149 | | waiter.Dispose(); |
| | 0 | 150 | | waiter = next; |
| | 0 | 151 | | } |
| | 0 | 152 | | while (waiter != _waitersTail); |
| | | 153 | | |
| | 0 | 154 | | _waitersTail = null; |
| | 0 | 155 | | } |
| | 0 | 156 | | } |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | private int TryRequestCreditNoLock(int amount) |
| | 0 | 160 | | { |
| | 0 | 161 | | Debug.Assert(Monitor.IsEntered(SyncObject), "Shouldn't be called outside lock."); |
| | | 162 | | |
| | 0 | 163 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 164 | | |
| | 0 | 165 | | if (_current > 0) |
| | 0 | 166 | | { |
| | 0 | 167 | | Debug.Assert(_waitersTail is null, "Shouldn't have waiters when credit is available"); |
| | | 168 | | |
| | 0 | 169 | | int granted = Math.Min(amount, _current); |
| | 0 | 170 | | if (NetEventSource.Log.IsEnabled()) _owner.Trace($"{_name}. requested={amount}, current={_current}, gran |
| | 0 | 171 | | _current -= granted; |
| | 0 | 172 | | return granted; |
| | | 173 | | } |
| | 0 | 174 | | return 0; |
| | 0 | 175 | | } |
| | | 176 | | } |
| | | 177 | | } |