-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_token_bucket_rate_limiter.cpp
More file actions
74 lines (63 loc) · 1.91 KB
/
ts_token_bucket_rate_limiter.cpp
File metadata and controls
74 lines (63 loc) · 1.91 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
#include <chrono>
#include <mutex>
#include <thread>
using namespace std;
/*
Problem:
Implement a thread-safe token bucket rate limiter.
Requirements:
1. Support multiple threads calling tryAcquire().
2. Rate = tokens added per second; capacity = max burst size.
3. Token refill should be based on elapsed time, not a background thread.
4. Keep the implementation and demo in a single file.
Follow-ups to practice:
- Add a blocking acquire(timeout).
- Add per-client buckets on top of a global bucket.
- Add metrics for granted/denied requests.
*/
class TokenBucketRateLimiter {
double tokens = 0.0;
double capacity = 0.0;
double refill_rate_per_sec = 0.0;
chrono::steady_clock::time_point last_refill = chrono::steady_clock::now();
mutex mtx;
void refillLocked() {
// TODO:
// 1. Compute elapsed time since last_refill.
// 2. Add elapsed * refill_rate_per_sec to tokens.
// 3. Cap at capacity.
// 4. Update last_refill.
}
public:
TokenBucketRateLimiter(double rate_per_sec, double cap)
: tokens(cap), capacity(cap), refill_rate_per_sec(rate_per_sec) {}
bool tryAcquire(double cost = 1.0) {
(void)cost;
// TODO:
// 1. Lock.
// 2. Refill tokens.
// 3. If enough tokens, deduct and return true.
// 4. Otherwise return false.
return false;
}
double approxTokens() {
// TODO:
// Optionally lock + refill, then return current token count.
return 0.0;
}
};
void client(TokenBucketRateLimiter& limiter, int id, int requests, int delay_ms) {
(void)limiter;
(void)id;
(void)requests;
(void)delay_ms;
// TODO:
// Simulate a caller issuing requests and printing ALLOWED/THROTTLED.
}
int main() {
// TODO:
// 1. Create limiter (rate + capacity).
// 2. Spawn multiple client threads.
// 3. Join threads and print final state.
return 0;
}