/
Limiter.cs
141 lines (117 loc) · 4.4 KB
/
Limiter.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Limiter - Limits calls per interval and concurrency
Copyright (c) 2012 Dror Gluska
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
(LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The terms of redistributing and/or modifying this software also
include exceptions to the LGPL that facilitate static linking.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
This class can be easily changed to be a generic class but will have to
drop the Stream inheritance, if you require multiple writes/reads/peeks, feel free.
Change log:
2012-08-03 - Initial version
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace GoogleAnalyticsDemo
{
/// <summary>
/// Class which allowes you control over how many calls per interval and how many concurrent calls
/// <para>Works like a regular lock.</para>
/// </summary>
public class Limiter
{
/// <summary>
/// Holds the latest datetime in which the requests per interval is valid
/// </summary>
private DateTime _last;
/// <summary>
/// Interval in which we're limiting the calls.
/// </summary>
private TimeSpan _interval;
/// <summary>
/// current calls per interval, this is zeroes each interval.
/// </summary>
private volatile int _callsperinterval;
/// <summary>
/// Maximum calls per interval
/// </summary>
private volatile int _maxcallsperinterval;
/// <summary>
/// Concurrent requests counter
/// </summary>
private volatile int _concurrentcalls;
/// <summary>
/// Maximum concurrent calls counter
/// </summary>
private volatile int _maxconcurrentcalls;
/// <summary>
/// Lock for calls counters.
/// </summary>
private SpinLock _callslock = new SpinLock();
/// <summary>
/// Initializes a new instance of the Limiter class
/// </summary>
/// <param name="interval">time span for maxRequestsPerInterval</param>
/// <param name="maxCallsPerInterval">maximum calls per interval</param>
/// <param name="maxConcurrent">maximum concurrent calls</param>
public Limiter(TimeSpan interval, int maxCallsPerInterval, int maxConcurrent)
{
_interval = interval;
_last = DateTime.Now + _interval;
_callsperinterval = 0;
_maxcallsperinterval = maxCallsPerInterval;
_concurrentcalls = 0;
_maxconcurrentcalls = maxConcurrent;
}
/// <summary>
/// Acquire a call, blocks if not available until available.
/// </summary>
public void Enter()
{
while (true)
{
var locktaken = false;
_callslock.Enter(ref locktaken);
if (_last < DateTime.Now)
{
_last = DateTime.Now + _interval;
_callsperinterval = 0;
}
//check interval
//check concurrent
if ((_concurrentcalls < _maxconcurrentcalls) && (_callsperinterval < _maxcallsperinterval))
{
Interlocked.Increment(ref _concurrentcalls);
Interlocked.Increment(ref _callsperinterval);
_callslock.Exit();
break;
}
_callslock.Exit();
Thread.Sleep(1);
}
}
/// <summary>
/// Releases a call
/// </summary>
public void Exit()
{
//reduce concurrent
var locktaken = false;
_callslock.Enter(ref locktaken);
Interlocked.Decrement(ref _concurrentcalls);
_callslock.Exit();
}
}
}