1 package org.cacert.gigi.util;
3 import java.util.HashMap;
4 import java.util.TreeSet;
6 public class RateLimit {
8 private class Entry implements Comparable<Entry> {
16 public Entry(long firstAccess, String feature) {
17 this.firstAccess = firstAccess;
18 this.feature = feature;
21 public void access() {
26 public int compareTo(Entry o) {
27 return feature.compareTo(o.feature);
30 public boolean isExpired() {
31 return firstAccess + time < System.currentTimeMillis();
36 private final int maxcount;
38 private final long time;
40 TreeSet<Entry> set = new TreeSet<Entry>();
42 HashMap<String, Entry> feat = new HashMap<>();
44 public RateLimit(int maxcount, long time) {
45 this.maxcount = maxcount;
49 public synchronized boolean isLimitExceeded(String feature) {
51 Entry e = feat.get(feature);
53 e = new Entry(System.currentTimeMillis(), feature);
59 return e.count > maxcount;
62 private void clean() {
63 while (set.size() > 0) {
67 feat.remove(e.feature);
74 public synchronized void bypass() {