forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSafeRateLimiter.java
More file actions
43 lines (36 loc) · 1.44 KB
/
ThreadSafeRateLimiter.java
File metadata and controls
43 lines (36 loc) · 1.44 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
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadSafeRateLimiter {
private final int maxRequests;
private final long timeWindowMillis;
private final AtomicInteger requestCount = new AtomicInteger(0);
private long windowStartTime;
public ThreadSafeRateLimiter(int maxRequests, long timeWindowMillis) {
this.maxRequests = maxRequests;
this.timeWindowMillis = timeWindowMillis;
this.windowStartTime = System.currentTimeMillis();
}
// Returns true if the request is allowed, false if rate limit exceeded
public synchronized boolean allowRequest() {
long now = System.currentTimeMillis();
if (now - windowStartTime > timeWindowMillis) {
// reset the window
requestCount.set(0);
windowStartTime = now;
}
if (requestCount.get() < maxRequests) {
requestCount.incrementAndGet();
return true;
} else {
return false;
}
}
// Test simulation
public static void main(String[] args) throws InterruptedException {
ThreadSafeRateLimiter limiter = new ThreadSafeRateLimiter(5, 1000); // 5 req/sec
for (int i = 1; i <= 10; i++) {
boolean allowed = limiter.allowRequest();
System.out.println("Request " + i + " -> " + (allowed ? "ALLOWED" : "BLOCKED"));
Thread.sleep(150); // simulate 150ms between requests
}
}
}