Skip to content

Commit d1bf7d5

Browse files
committed
Add optimization
1 parent 9d2f9ce commit d1bf7d5

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

agent_api/src/main/java/dev/aikido/agent_api/ratelimiting/SlidingWindowRateLimiter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public boolean isAllowed(String key, long windowSizeInMs, long maxRequests) {
2323
// clear entries that are not in the rate-limiting window anymore
2424
requestTimestamps.removeIf(entry -> entry < currentTime - windowSizeInMs);
2525

26+
// Ensure the number of entries exceeds maxRequests by only 1
27+
while (requestTimestamps.size() > (maxRequests+1)) {
28+
// We remove the oldest entry, since this one has become useless if the limit is already exceeded
29+
requestTimestamps.remove(0);
30+
}
31+
2632
// Update entries by adding the new timestamp and storing it in the LRU Cache
2733
requestTimestamps.add(currentTime);
2834
rateLimitedItems.set(key, requestTimestamps);

0 commit comments

Comments
 (0)