Only inspect the last threshold.seconds when determining if there is a breach. Otherwise older breaches would be counted as current

This commit is contained in:
zab2
2019-04-06 15:35:20 +00:00
parent 8a001adf59
commit 904bf2a90b
3 changed files with 8 additions and 3 deletions

View File

@@ -28,16 +28,20 @@ class AccessCounter {
/**
* @param threshold definition of a threshold
* @param now current time
* @return true if the given threshold has been breached
*/
boolean isBreached(Threshold threshold) {
boolean isBreached(Threshold threshold, long now) {
if (threshold.getConnections() == 0)
return !accesses.isEmpty();
if (accesses.size() < threshold.getConnections())
return false;
long ignoreOlder = now - threshold.getSeconds() * 1000;
for (int i = 0; i <= accesses.size() - threshold.getConnections(); i++) {
long start = accesses.get(i);
if (start < ignoreOlder)
continue;
long end = start + threshold.getSeconds() * 1000;
if (accesses.get(i + threshold.getConnections() -1) <= end)
return true;

View File

@@ -123,13 +123,14 @@ class AccessFilter implements StatefulConnectionFilter {
}
private void record() throws IOException {
final long now = context.clock().now();
for (Recorder recorder : definition.getRecorders()) {
Threshold threshold = recorder.getThreshold();
File file = recorder.getFile();
Set<String> breached = new HashSet<String>();
synchronized(unknownDests) {
for (DestTracker tracker : unknownDests.values()) {
if (!tracker.getCounter().isBreached(threshold))
if (!tracker.getCounter().isBreached(threshold, now))
continue;
breached.add(tracker.getHash().toBase32());
}

View File

@@ -36,7 +36,7 @@ class DestTracker {
*/
synchronized boolean recordAccess(long now) {
counter.recordAccess(now);
return counter.isBreached(threshold);
return counter.isBreached(threshold,now);
}
synchronized boolean purge(long olderThan) {