-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRouterRpcGate.cs
More file actions
198 lines (161 loc) · 5.02 KB
/
RouterRpcGate.cs
File metadata and controls
198 lines (161 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Threading;
using System.Threading.Tasks;
namespace net.vieapps.Services
{
public sealed class RouterRpcGate
{
int _hardMax;
int _softMax;
int _inflight;
readonly SemaphoreSlim _semaphore;
readonly int _timeoutMilliseconds;
readonly object _locker = new object();
readonly int _increaseIntervalMilliseconds = 150;
readonly int _cooldownMilliseconds = 2000;
readonly int _increaseStep = 1;
int _ticking;
Timer _timer;
long _lastIncreaseTicks;
long _lastDecreaseTicks;
public readonly struct Releaser : IDisposable
{
readonly RouterRpcGate _rpcgate;
readonly int _weight;
readonly bool _usedSemaphore;
internal Releaser(RouterRpcGate rpcgate, int weight, bool usedSemaphore)
{
this._rpcgate = rpcgate;
this._weight = weight;
this._usedSemaphore = usedSemaphore;
}
public void Dispose() => this._rpcgate?.Release(this._weight, this._usedSemaphore);
}
public int Current => Volatile.Read(ref this._inflight);
public int Max => Volatile.Read(ref this._softMax);
public int Available => this.Max - this.Current;
public double Usage => (double)this.Current / Math.Max(1, this.Max);
public RouterRpcGate(int max, int timeoutMilliseconds = 50)
{
if (max <= 0)
throw new ArgumentOutOfRangeException(nameof(max));
if (timeoutMilliseconds < 0)
throw new ArgumentOutOfRangeException(nameof(timeoutMilliseconds));
this._hardMax = max;
this._softMax = max;
this._timeoutMilliseconds = timeoutMilliseconds;
this._semaphore = new SemaphoreSlim(max, Int32.MaxValue);
}
public ValueTask<Releaser?> TryEnterAsync(CancellationToken cancellationToken = default)
=> this.TryEnterAsync(1, cancellationToken);
public async ValueTask<Releaser?> TryEnterAsync(int weight, CancellationToken cancellationToken)
{
if (weight <= 0)
throw new ArgumentOutOfRangeException(nameof(weight));
while (true)
{
var current = Volatile.Read(ref this._inflight);
var capacity = Volatile.Read(ref this._softMax);
if (weight > capacity - current)
break;
if (Interlocked.CompareExchange(ref this._inflight, current + weight, current) == current)
return new Releaser(this, weight, false);
}
if (this._timeoutMilliseconds <= 0)
return null;
if (!await this._semaphore.WaitAsync(this._timeoutMilliseconds, cancellationToken).ConfigureAwait(false))
return null;
while (true)
{
var current = Volatile.Read(ref this._inflight);
var capacity = Volatile.Read(ref this._softMax);
if (weight > capacity - current)
{
this._semaphore.Release();
return null;
}
if (Interlocked.CompareExchange(ref this._inflight, current + weight, current) == current)
return new Releaser(this, weight, true);
}
}
internal void Release(int weight, bool usedSemaphore)
{
Interlocked.Add(ref this._inflight, -weight);
if (usedSemaphore)
this._semaphore.Release();
}
public void SetMaxCapacity(int newMaxCapacity)
{
if (newMaxCapacity <= 0)
throw new ArgumentOutOfRangeException(nameof(newMaxCapacity));
Volatile.Write(ref this._hardMax, newMaxCapacity);
var softMax = Volatile.Read(ref this._softMax);
if (newMaxCapacity < softMax)
{
Volatile.Write(ref this._softMax, newMaxCapacity);
Volatile.Write(ref this._lastDecreaseTicks, this.TickCount);
}
else if (newMaxCapacity > softMax)
this.StartTimer();
}
void StartTimer()
{
var timer = Volatile.Read(ref this._timer);
if (timer == null)
{
lock (this._locker)
{
if (this._timer == null)
this._timer = new Timer(_ => this.OnTimerTick(), null, this._increaseIntervalMilliseconds, this._increaseIntervalMilliseconds);
}
}
}
void StopTimer()
{
lock (this._locker)
{
var timer = this._timer;
this._timer = null;
timer?.Dispose();
}
}
void OnTimerTick()
{
if (Interlocked.Exchange(ref this._ticking, 1) == 1)
return;
try
{
var now = this.TickCount;
var hardMax = Volatile.Read(ref this._hardMax);
var softMax = Volatile.Read(ref this._softMax);
if (softMax >= hardMax)
{
this.StopTimer();
return;
}
if (!RouterRpcGate.Elapsed(now, Volatile.Read(ref this._lastDecreaseTicks), this._cooldownMilliseconds))
return;
if (!RouterRpcGate.Elapsed(now, Volatile.Read(ref this._lastIncreaseTicks), this._increaseIntervalMilliseconds))
return;
var inflight = Volatile.Read(ref this._inflight);
if (inflight >= (int)(softMax * 0.8))
return;
var next = Math.Min(hardMax, softMax + this._increaseStep);
Volatile.Write(ref this._softMax, next);
Volatile.Write(ref this._lastIncreaseTicks, now);
}
finally
{
this._ticking = 0;
}
}
#if NETSTANDARD2_0
long TickCount
=> (long)Environment.TickCount;
static bool Elapsed(long now, long last, int milliseconds) => unchecked((int)(now - last)) >= milliseconds;
#else
long TickCount => Environment.TickCount64;
static bool Elapsed(long now, long last, int milliseconds) => (now - last) >= milliseconds;
#endif
}
}