com.aol.micro.server.events.LoggingRateLimiter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micro-events Show documentation
Show all versions of micro-events Show documentation
Opinionated rest microservices
package com.aol.micro.server.events;
import java.util.Date;
import com.google.common.collect.ConcurrentHashMultiset;
import lombok.AccessLevel;
import lombok.Getter;
public class LoggingRateLimiter {
@Getter(AccessLevel.PACKAGE)
private volatile ConcurrentHashMultiset frequency = ConcurrentHashMultiset.create();
private volatile Date lastCleared = new Date(
0);
private final int limit;
public LoggingRateLimiter(int limit) {
this.limit = limit;
}
public LoggingRateLimiter() {
this.limit = (60 * 1000 * 60);
}
public void addAndEnsureFrequency(T clazz) {
resetAfterLimit();
frequency.add(clazz);
}
public void resetAfterLimit() {
if (System.currentTimeMillis() - limit > lastCleared.getTime()) {
frequency = ConcurrentHashMultiset.create();
lastCleared = new Date(
System.currentTimeMillis());
}
}
public void capacityAvailable(T t, long max, Runnable run) {
if (frequency.count(t) < max)
run.run();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy