-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRateLimiter.cs
More file actions
254 lines (217 loc) · 9.66 KB
/
RateLimiter.cs
File metadata and controls
254 lines (217 loc) · 9.66 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using Decepticon.RateLimit.Redis.Helpers;
using StackExchange.Redis;
namespace Decepticon.RateLimit
{
/// <summary>
/// Provide functions to rate limit. Each function uses different
/// rate limit algoritms to fit your application's needs.
/// Currently supports Fixed Window and Token Bucket algorithms.
/// </summary>
public class RateLimiter
{
private readonly IDatabase _Database;
/// <summary>
/// Create a new rate limiter instance from a Redis <see cref="IDatabase"/>
/// </summary>
/// <param name="database"></param>
public RateLimiter(IDatabase database)
{
_Database = database ?? throw new ArgumentNullException(nameof(database));
}
#region Fixed window
/// <summary>
/// Validates to check if the request is allowed using the fixed window algorithm.
/// Use this if you want to allow x requests per day/hour for a specific user, client, or application.
/// See <seealso cref="FixedRateLimitRequest"/>.
/// Throws <c>ArgumentException</c>
/// </summary>
/// <remarks>This will not prevent traffic spikes. If you want to do request throttling to protect your service, use the other overload.</remarks>
/// <param name="request"></param>
/// <returns></returns>
public FixedRateLimitResult Validate(FixedRateLimitRequest request)
{
if (string.IsNullOrWhiteSpace(request.Key))
{
throw new ArgumentException($"{nameof(request.Key)} identifier must be specified");
}
else if (request.Capacity <= 0)
{
throw new ArgumentException($"{nameof(request.Capacity)} must be greater than zero");
}
else if (request.WindowSize <= 0)
{
throw new ArgumentException($"{nameof(request.WindowSize)} must be greater than zero");
}
// Convert the window to ticks from seconds
var windowSizeInTicks = request.WindowSize * TimeSpan.TicksPerSecond;
// Get current global time from data server like MongoDb, Redis, or SQL server
var currentTimeInTicks = GetCurrentTimeInTicks();
var keys = new RedisKey[]
{
$"{Constants.RateLimitCategory}:{Constants.FixedRateCategory}:{request.Key}:count",
$"{Constants.RateLimitCategory}:{Constants.FixedRateCategory}:{request.Key}:ticks",
};
var values = new RedisValue[]
{
$"{currentTimeInTicks}",
$"{windowSizeInTicks}",
$"{request.Capacity}"
};
const string script = @"
-- Variables
local tonumber = tonumber
local currentTimeTicks = tonumber(ARGV[1])
local windowSizeTicks = tonumber(ARGV[2])
local capacity = tonumber(ARGV[3])
local count = 0
local ticks = currentTimeTicks
local result = 1
-- Try getting tracking config from existing record
local countStr = redis.call('GET', KEYS[1])
if countStr then
count = tonumber(countStr)
end
local tickStr = redis.call('GET', KEYS[2])
if tickStr then
ticks = tonumber(tickStr)
end
-- Increment the counter
count = count + 1
-- Check the count if request is within the window. Set to denied if greater than the allowe capacity
if (currentTimeTicks - ticks) < windowSizeTicks then
if count > capacity then
result = 0
end
-- Otherwise, reset. Count set to 1 because this current request counts.
else
count = 1
ticks = currentTimeTicks
end
redis.call('SET', KEYS[1], count)
redis.call('SET', KEYS[2], ticks)
return result .. ',' .. math.abs(currentTimeTicks - ticks - windowSizeTicks)
";
return ToFixedRateLimitResult(_Database.ScriptEvaluate(script, keys, values).ToString());
}
private FixedRateLimitResult ToFixedRateLimitResult(string result)
{
var results = result.Split(',');
var allowed = results[0] != "0";
return new FixedRateLimitResult
{
IsAllowed = allowed,
ResetAfter = allowed
? 0
// Convert to human readable unit
: (double.Parse(results[1]) / TimeSpan.TicksPerSecond)
};
}
#endregion
#region Throttling
/// <summary>
/// Validates to check if the request is allowed using the Token Bucket algorithm.
/// Use this if you want to prevent spikes by smoothing it out but with some burstiness allowed.
/// See <seealso cref="ThrottleRateLimitRequest"/>.
/// Throws <c>ArgumentException</c>
/// </summary>
/// <remarks>If you want to do daily/hour limit, use the other overload.</remarks>
/// <param name="request"></param>
/// <returns></returns>
public ThrottleRateLimitResult Validate(ThrottleRateLimitRequest request)
{
if (string.IsNullOrWhiteSpace(request.Key))
{
throw new ArgumentException($"{nameof(request.Key)} identifier must be specified");
}
else if (request.Capacity <= 0)
{
throw new ArgumentException($"{nameof(request.Capacity)} must be greater than zero");
}
else if (request.RefillRate <= 0)
{
throw new ArgumentException($"{nameof(request.RefillRate)} must be greater than zero");
}
// Get current global time from data server like MongoDb, Redis, or SQL server
var currentTimeInTicks = GetCurrentTimeInTicks();
var keys = new RedisKey[]
{
$"{Constants.RateLimitCategory}:{Constants.ThrottlingCategory}:{request.Key}:count",
$"{Constants.RateLimitCategory}:{Constants.ThrottlingCategory}:{request.Key}:ticks",
};
var values = new RedisValue[]
{
$"{currentTimeInTicks}",
$"{request.RefillRate}",
$"{request.Capacity}"
};
const string script = @"
-- Variables
local tonumber = tonumber
local currentTimeTicks = tonumber(ARGV[1])
local refillRate = tonumber(ARGV[2])
local capacity = tonumber(ARGV[3])
local count = tonumber(ARGV[3])
local ticks = currentTimeTicks
local result = 1
local ttl = 86400
-- Try getting tracking config from existing record
local countStr = redis.call('GET', KEYS[1])
if countStr then
count = tonumber(countStr)
end
local tickStr = redis.call('GET', KEYS[2])
if tickStr then
ticks = tonumber(tickStr)
end
-- Refill, take the difference between the last time it refill and now
-- then divide by ticks in a second
local tokensToAdd = math.floor((currentTimeTicks - ticks) / 10000000) * refillRate
count = count + tokensToAdd
-- Update the timestamp when we have a token refill
if tokensToAdd > 0 then
ticks = currentTimeTicks
end
-- Tokens are maxed at the capacity
if count > capacity then
count = capacity
end
-- Consume a token
count = count - 1
-- Get seconds for a complete refill, and then add 1hr to prevent deleting keys too frequently
ttl = (capacity / refillRate) + 3600
-- Determine the outcome
if count < 0 then
result = 0
count = 0
end
redis.call('SET', KEYS[1], count, 'EX', ttl)
redis.call('SET', KEYS[2], ticks, 'EX', ttl)
return result .. ',' .. math.abs(10000000 / refillRate)
";
return ToThrottleRateLimitResult(_Database.ScriptEvaluate(script, keys, values).ToString());
}
private ThrottleRateLimitResult ToThrottleRateLimitResult(string result)
{
var results = result.Split(',');
var allowed = results[0] != "0";
return new ThrottleRateLimitResult
{
IsAllowed = allowed,
RetryAfter = allowed
? 0
// Convert to human readable unit
: (double.Parse(results[1]) / TimeSpan.TicksPerSecond)
};
}
#endregion
private long GetCurrentTimeInTicks()
{
// Call the time function and get the current time in seconds
var unixTimestampInSeconds = _Database.ScriptEvaluate("return redis.call('TIME')[1]")
.ToString();
return DateTimeHelpers.UnixTimeStampToDateTime(double.Parse(unixTimestampInSeconds))
.Ticks;
}
}
}