-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01.DesignARateLimiter.java
More file actions
169 lines (146 loc) · 5.75 KB
/
01.DesignARateLimiter.java
File metadata and controls
169 lines (146 loc) · 5.75 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
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class RateLimiter {
private final int defaultMaxRequests;
private final double defaultWindowSeconds;
private final Map<String, Deque<Long>> store;
private final Map<String, ReentrantLock> locks;
private final Map<String, Limit> limits;
private static class Limit {
int maxRequests;
double windowSeconds;
Limit(int maxRequests, double windowSeconds) {
this.maxRequests = maxRequests;
this.windowSeconds = windowSeconds;
}
}
public RateLimiter(int defaultMaxRequests, double defaultWindowSeconds) {
this.defaultMaxRequests = defaultMaxRequests;
this.defaultWindowSeconds = defaultWindowSeconds;
this.store = new ConcurrentHashMap<>();
this.locks = new ConcurrentHashMap<>();
this.limits = new ConcurrentHashMap<>();
}
public void setLimit(String key, int maxRequests, double windowSeconds) {
limits.put(key, new Limit(maxRequests, windowSeconds));
}
private Limit getLimit(String key) {
return limits.getOrDefault(key, new Limit(defaultMaxRequests, defaultWindowSeconds));
}
public boolean allowRequest(String key) {
long now = System.nanoTime();
Limit limit = getLimit(key);
double windowNanos = limit.windowSeconds * 1e9;
ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock());
lock.lock();
try {
Deque<Long> q = store.computeIfAbsent(key, k -> new ArrayDeque<>());
long boundary = now - (long) windowNanos;
while (!q.isEmpty() && q.peekFirst() <= boundary) {
q.pollFirst();
}
if (q.size() < limit.maxRequests) {
q.addLast(now);
return true;
} else {
return false;
}
} finally {
lock.unlock();
}
}
public synchronized Usage getUsage(String key) {
long now = System.nanoTime();
Limit limit = getLimit(key);
double windowNanos = limit.windowSeconds * 1e9;
ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock());
lock.lock();
try {
Deque<Long> q = store.computeIfAbsent(key, k -> new ArrayDeque<>());
long boundary = now - (long) windowNanos;
while (!q.isEmpty() && q.peekFirst() <= boundary) {
q.pollFirst();
}
int count = q.size();
double ttl = 0.0;
if (!q.isEmpty()) {
long oldest = q.peekFirst();
ttl = ((oldest + windowNanos) - now) / 1e9;
if (ttl < 0) ttl = 0;
}
return new Usage(count, ttl);
} finally {
lock.unlock();
}
}
static class Usage {
int count;
double ttl;
Usage(int count, double ttl) {
this.count = count;
this.ttl = ttl;
}
}
// Worker to simulate requests (same as Python demo)
static class Worker implements Runnable {
private final RateLimiter limiter;
private final String key;
private final int attempts;
private final double pause;
private final List<String> results;
Worker(RateLimiter limiter, String key, int attempts, double pause, List<String> results) {
this.limiter = limiter;
this.key = key;
this.attempts = attempts;
this.pause = pause;
this.results = results;
}
@Override
public void run() {
for (int i = 0; i < attempts; i++) {
boolean allowed = limiter.allowRequest(key);
Usage usage = limiter.getUsage(key);
String res = Thread.currentThread().getId() +
" attempt " + i + ": " +
(allowed ? "ALLOWED" : "REJECTED") +
" | in-window=" + usage.count +
" ttl=" + String.format("%.3f", usage.ttl) + "s";
synchronized (results) {
results.add(res);
}
try {
Thread.sleep((long) (pause * 1000));
} catch (InterruptedException ignored) {}
}
}
}
public static void main(String[] args) throws Exception {
RateLimiter limiter = new RateLimiter(5, 2.0); // 5 requests per 2 seconds
String key = "user:alice";
List<String> results = Collections.synchronizedList(new ArrayList<>());
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int t = 0; t < 3; t++) {
executor.submit(new Worker(limiter, key, 4, 0.2, results));
}
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
for (String s : results) {
System.out.println(s);
}
}
}