-
Notifications
You must be signed in to change notification settings - Fork 25
/
RateLimitCompactionFilter.cpp
36 lines (26 loc) · 1.21 KB
/
RateLimitCompactionFilter.cpp
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
#include "ratelimit/RateLimitCompactionFilter.h"
#include <algorithm>
#include <chrono>
#include <string>
#include "glog/logging.h"
#include "pipeline/RedisHandler.h"
#include "ratelimit/RateLimitHandler.h"
namespace ratelimit {
bool RateLimitCompactionFilter::Filter(int level, const rocksdb::Slice& key, const rocksdb::Slice& existingValue,
std::string* newValue, bool* valueChanged) const {
*valueChanged = false;
RateLimitHandler::KeyParams keyParams;
CHECK(RateLimitHandler::decodeRateLimitKey(key, &keyParams)) << "RateLimit key in RocksDB is corrupted";
RateLimitHandler::ValueParams valueParams;
CHECK(RateLimitHandler::decodeRateLimitValue(existingValue, &valueParams, nullptr))
<< "RateLimit value in RocksDB is corrupted";
RedisIntType nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
RedisIntType idleTimeMs = nowMs - valueParams.lastReducedAtMs;
if (idleTimeMs / keyParams.refillTimeMs * keyParams.refillAmount >= keyParams.maxAmount) {
// we would have a full bucket anyway, so no longer need the key
return true;
}
return false;
}
} // namespace ratelimit