X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Futil%2FRateLimit.java;fp=src%2Forg%2Fcacert%2Fgigi%2Futil%2FRateLimit.java;h=0000000000000000000000000000000000000000;hp=d7873a03830cd20877c4e76cf3bba4024bb1bc02;hb=bccd4cc0dba0f89aa045b113bac46eb8cc1dab4e;hpb=c9ed09f0007fc2c813815be927a5a24b23dab83c diff --git a/src/org/cacert/gigi/util/RateLimit.java b/src/org/cacert/gigi/util/RateLimit.java deleted file mode 100644 index d7873a03..00000000 --- a/src/org/cacert/gigi/util/RateLimit.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.cacert.gigi.util; - -import java.util.HashMap; -import java.util.TreeSet; - -import org.cacert.gigi.GigiApiException; - -public class RateLimit { - - public static final class RateLimitException extends GigiApiException { - - private static final long serialVersionUID = 1L; - - public RateLimitException() { - super("Rate limit exceeded."); - } - } - - private class Entry implements Comparable { - - long firstAccess; - - int count = 1; - - String feature; - - public Entry(long firstAccess, String feature) { - this.firstAccess = firstAccess; - this.feature = feature; - } - - public void access() { - count++; - } - - @Override - public int compareTo(Entry o) { - return feature.compareTo(o.feature); - } - - public boolean isExpired() { - return firstAccess + time < System.currentTimeMillis(); - } - - } - - private final int maxcount; - - private final long time; - - TreeSet set = new TreeSet(); - - HashMap feat = new HashMap<>(); - - public RateLimit(int maxcount, long time) { - this.maxcount = maxcount; - this.time = time; - } - - public synchronized boolean isLimitExceeded(String feature) { - clean(); - Entry e = feat.get(feature); - if (e == null) { - e = new Entry(System.currentTimeMillis(), feature); - set.add(e); - feat.put(feature, e); - } else { - e.access(); - } - return e.count > maxcount; - } - - private void clean() { - while (set.size() > 0) { - Entry e = set.last(); - if (e.isExpired()) { - set.remove(e); - feat.remove(e.feature); - } else { - return; - } - } - } - - public synchronized void bypass() { - set.clear(); - feat.clear(); - } -}