-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenBucket.cpp
More file actions
55 lines (46 loc) · 1.37 KB
/
TokenBucket.cpp
File metadata and controls
55 lines (46 loc) · 1.37 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
#include <chrono>
#include <iostream>
#include <thread>
// https://cloud.tencent.com/developer/article/2375099
// 也可以增加个变量记录上次访问时间,在consume时前面刷新tokens_数量
class TokenBucket {
public:
TokenBucket(int capacity, int rate)
: capacity_(capacity), rate_(rate), tokens_(capacity) {}
bool consume(int tokens) {
if (tokens <= tokens_) {
tokens_ -= tokens;
return true;
} else {
return false;
}
}
void refill() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
tokens_ = std::min(capacity_, tokens_ + rate_);
}
}
private:
int capacity_;
int rate_;
int tokens_;
};
int main() {
TokenBucket bucket(10, 2); // 令牌桶容量为10,每秒增加2个令牌
// 启动令牌桶的自动补充线程
std::thread refill_thread([&] { bucket.refill(); });
// 模拟资源访问
for (int i = 0; i < 20; ++i) {
if (bucket.consume(1)) {
std::cout << "Access granted" << std::endl;
} else {
std::cout << "Access denied" << std::endl;
}
std::this_thread::sleep_for(
std::chrono::milliseconds(500)); // 模拟资源访问间隔
}
// 等待自动补充线程结束
refill_thread.join();
return 0;
}