Has Anyone recently tried implementing Rate Limiter?
Basic requirement :
I found some code but unable to understand how can I make this memory efficient or give a functionality on how to reset data. For example: enteries which are present System.currentTime - 5000ms is stale and should be removed from data structure we are using.
I checked ring buffer(https://en.wikipedia.org/wiki/Circular_buffer) has anyone tried implementing the same??
public class UserFixedWindowCounter {
private final int REQUEST_LIMIT = 3;
private final long TIME_LIMIT = 1000L;
public class HitCounter{
Queue<Long> q;
HitCounter(){
q = new LinkedList<>();
}
public boolean hit(long timestamp) {
while (!q.isEmpty() && timestamp - q.peek() >= TIME_LIMIT)
q.peek();
if (q.size() < REQUEST_LIMIT) {
q.offer(timestamp);
return true;
}
return false;
}
}
public boolean isAllow(String clientId){
HashMap<String, HitCounter> map = new HashMap<>();
HitCounter h = map.get(clientId);
long currTimeStamp = System.currentTimeMillis();
if (h == null) {
h = new HitCounter();
map.put(clientId, h);
}
return h.hit(currTimeStamp);
}
}