-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathratelimit.h
50 lines (42 loc) · 988 Bytes
/
ratelimit.h
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
#ifndef __RATELIMITER__HH__
#define __RATELIMITER__HH__
#include <string>
#include <unordered_map>
#include <mutex>
#include <shared_mutex>
class RateLimiter {
private:
std::unordered_map<uint64_t, uint32_t> limiters;
mutable std::shared_mutex m; // Mutex for hashmap updates
uint32_t hits_per_second;
std::thread refiller;
volatile bool end;
public:
// Checks whether we hit the limit
bool check(uint64_t iphash) const {
std::shared_lock lock(m);
return (limiters.count(iphash) && limiters.at(iphash) > hits_per_second);
}
// Accounts one access to the given access ID
void consume(uint64_t iphash) {
std::unique_lock lock(m);
limiters[iphash]++;
}
RateLimiter(unsigned maxhps)
: hits_per_second(maxhps), end(false) {
refiller = std::thread([this] {
while (!this->end) {
sleep(1);
if (!this->end) {
std::unique_lock lock(m);
this->limiters.clear();
}
}
});
}
~RateLimiter() {
end = true;
refiller.join();
}
};
#endif