Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CreditManager.cs
Go to the documentation of this file.
3
4namespace System.Net.Http;
5
6internal sealed class CreditManager
7{
8 private readonly IHttpTrace _owner;
9
10 private readonly string _name;
11
12 private int _current;
13
14 private bool _disposed;
15
17
18 public bool IsCreditAvailable => Volatile.Read(ref _current) > 0;
19
20 private object SyncObject => this;
21
22 public CreditManager(IHttpTrace owner, string name, int initialCredit)
23 {
24 if (System.Net.NetEventSource.Log.IsEnabled())
25 {
26 owner.Trace($"{name}. {"initialCredit"}={initialCredit}", ".ctor");
27 }
28 _owner = owner;
29 _name = name;
30 _current = initialCredit;
31 }
32
34 {
35 lock (SyncObject)
36 {
37 int num = TryRequestCreditNoLock(amount);
38 if (num > 0)
39 {
40 return new ValueTask<int>(num);
41 }
42 if (System.Net.NetEventSource.Log.IsEnabled())
43 {
44 _owner.Trace($"{_name}. requested={amount}, no credit available.", "RequestCreditAsync");
45 }
47 creditWaiter.Amount = amount;
48 if (_waitersTail == null)
49 {
50 _waitersTail = (creditWaiter.Next = creditWaiter);
51 }
52 else
53 {
54 creditWaiter.Next = _waitersTail.Next;
55 _waitersTail.Next = creditWaiter;
56 _waitersTail = creditWaiter;
57 }
58 return creditWaiter.AsValueTask();
59 }
60 }
61
62 public void AdjustCredit(int amount)
63 {
64 lock (SyncObject)
65 {
66 if (System.Net.NetEventSource.Log.IsEnabled())
67 {
68 _owner.Trace($"{_name}. {"amount"}={amount}, current={_current}", "AdjustCredit");
69 }
70 if (_disposed)
71 {
72 return;
73 }
74 checked
75 {
76 _current += amount;
77 }
78 while (_current > 0 && _waitersTail != null)
79 {
81 int num = Math.Min(next.Amount, _current);
82 if (next.Next == next)
83 {
84 _waitersTail = null;
85 }
86 else
87 {
88 _waitersTail.Next = next.Next;
89 }
90 next.Next = null;
91 if (next.TrySetResult(num))
92 {
93 _current -= num;
94 }
95 next.Dispose();
96 }
97 }
98 }
99
100 public void Dispose()
101 {
102 lock (SyncObject)
103 {
104 if (_disposed)
105 {
106 return;
107 }
108 _disposed = true;
109 CreditWaiter creditWaiter = _waitersTail;
110 if (creditWaiter != null)
111 {
112 do
113 {
114 CreditWaiter next = creditWaiter.Next;
115 creditWaiter.Next = null;
116 creditWaiter.Dispose();
117 creditWaiter = next;
118 }
119 while (creditWaiter != _waitersTail);
120 _waitersTail = null;
121 }
122 }
123 }
124
125 private int TryRequestCreditNoLock(int amount)
126 {
127 if (_disposed)
128 {
129 throw new ObjectDisposedException($"{"CreditManager"}:{_owner.GetType().Name}:{_name}");
130 }
131 if (_current > 0)
132 {
133 int num = Math.Min(amount, _current);
134 if (System.Net.NetEventSource.Log.IsEnabled())
135 {
136 _owner.Trace($"{_name}. requested={amount}, current={_current}, granted={num}", "TryRequestCreditNoLock");
137 }
138 _current -= num;
139 return num;
140 }
141 return 0;
142 }
143}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
ValueTask< int > RequestCreditAsync(int amount, CancellationToken cancellationToken)
int TryRequestCreditNoLock(int amount)
readonly IHttpTrace _owner
CreditManager(IHttpTrace owner, string name, int initialCredit)
ValueTask< int > AsValueTask()
bool TrySetResult(int result)
static readonly System.Net.NetEventSource Log
static bool Read(ref bool location)
Definition Volatile.cs:67
void Trace(string message, [CallerMemberName] string memberName=null)