-
Notifications
You must be signed in to change notification settings - Fork 0
/
Throttler.cs
125 lines (115 loc) · 4.12 KB
/
Throttler.cs
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
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Common.Engine
{
/// <summary>
/// 限流器
/// 改写自:hadoop\src\hdfs\org\apache\hadoop\hdfs\server\datanode\BlockTransferThrottler.java
/// </summary>
public class Throttler
{
private readonly int _period; // period over which bw is imposed
private readonly int _periodExtension; // Max period over which bw accumulates.
private int _bytesPerPeriod; // total number of bytes can be sent in each period
private int _curPeriodStart; // current period starting time
private int _curReserve; // remaining bytes can be sent in the period
private int _bytesAlreadyUsed;
/** Constructor
* @param bandwidthPerSec bandwidth allowed in bytes per second.
*/
public Throttler(int bandwidthPerSec): this(500, bandwidthPerSec)
{ // by default throttling period is 500ms
}
/**
* Constructor
* @param period in milliseconds. Bandwidth is enforced over this
* period.
* @param bandwidthPerSec bandwidth allowed in bytes per second.
*/
public Throttler(int period, int bandwidthPerSec)
{
this._curPeriodStart = CurrentTimeMillis();
this._period = period;
this._curReserve = this._bytesPerPeriod = bandwidthPerSec * period / 1000;
this._periodExtension = period * 3;
}
/// <summary>
/// 当前的时间戳
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.Synchronized)]
private static int CurrentTimeMillis()
{
return Environment.TickCount;
}
/**
* @return current throttle bandwidth in bytes per second.
*/
[MethodImpl(MethodImplOptions.Synchronized)]
public long GetBandwidth()
{
return _bytesPerPeriod * 1000 / _period;
}
/**
* Sets throttle bandwidth. This takes affect latest by the end of current
* period.
*
* @param bytesPerSecond
*/
[MethodImpl(MethodImplOptions.Synchronized)]
public void SetBandwidth(int bytesPerSecond)
{
if (bytesPerSecond <= 0)
{
throw new Exception("" + bytesPerSecond);
}
_bytesPerPeriod = bytesPerSecond * _period / 1000;
}
/** Given the numOfBytes sent/received since last time throttle was called,
* make the current thread sleep if I/O rate is too fast
* compared to the given bandwidth.
*
* @param numOfBytes
* number of bytes sent/received since last time throttle was called
*/
[MethodImpl(MethodImplOptions.Synchronized)]
public void Throttle(int numOfBytes)
{
if (numOfBytes <= 0)
{
return;
}
_curReserve -= numOfBytes;
_bytesAlreadyUsed += numOfBytes;
while (_curReserve <= 0)
{
int now = CurrentTimeMillis();
int curPeriodEnd = _curPeriodStart + _period;
if (now < curPeriodEnd)
{
// Wait for next period so that curReserve can be increased.
try
{
Thread.Sleep(curPeriodEnd - now);
}
catch (Exception)
{ }
}
else if (now < (_curPeriodStart + _periodExtension))
{
_curPeriodStart = curPeriodEnd;
_curReserve += _bytesPerPeriod;
}
else
{
// discard the prev period. Throttler might not have
// been used for a long time.
_curPeriodStart = now;
_curReserve = _bytesPerPeriod - _bytesAlreadyUsed;
}
}
_bytesAlreadyUsed -= numOfBytes;
}
}
}