"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "zxingorg/src/main/java/com/google/zxing/web/DoSTracker.java" between
zxing-zxing-3.4.1.tar.gz and zxing-zxing-3.5.0.tar.gz

About: ZXing ("zebra crossing") is a multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Info: Project is in maintenance mode (no active development).

DoSTracker.java  (zxing-zxing-3.4.1):DoSTracker.java  (zxing-zxing-3.5.0)
skipping to change at line 50 skipping to change at line 50
/** /**
* @param timer {@link Timer} to use for scheduling update tasks * @param timer {@link Timer} to use for scheduling update tasks
* @param name identifier for this tracker * @param name identifier for this tracker
* @param maxAccessesPerTime maximum number of accesses allowed from one sourc e per {@code accessTimeMS} * @param maxAccessesPerTime maximum number of accesses allowed from one sourc e per {@code accessTimeMS}
* @param accessTimeMS interval in milliseconds over which up to {@code maxAcc essesPerTime} accesses are allowed * @param accessTimeMS interval in milliseconds over which up to {@code maxAcc essesPerTime} accesses are allowed
* @param maxEntries maximum number of source entries to track before forgetti ng least recent ones * @param maxEntries maximum number of source entries to track before forgetti ng least recent ones
* @param maxLoad if set, dynamically adjust {@code maxAccessesPerTime} downwa rds when average load per core * @param maxLoad if set, dynamically adjust {@code maxAccessesPerTime} downwa rds when average load per core
* exceeds this value, and upwards when below this value * exceeds this value, and upwards when below this value
*/ */
DoSTracker(Timer timer, DoSTracker(Timer timer,
final String name, String name,
final int maxAccessesPerTime, int maxAccessesPerTime,
long accessTimeMS, long accessTimeMS,
int maxEntries, int maxEntries,
Double maxLoad) { Double maxLoad) {
this.maxAccessesPerTime = maxAccessesPerTime; this.maxAccessesPerTime = maxAccessesPerTime;
this.numRecentAccesses = new LRUMap<>(maxEntries); this.numRecentAccesses = new LRUMap<>(maxEntries);
timer.schedule(new TrackerTask(name, maxLoad), accessTimeMS, accessTimeMS); timer.schedule(new TrackerTask(name, maxLoad), accessTimeMS, accessTimeMS);
} }
boolean isBanned(String event) { boolean isBanned(String event) {
if (event == null) { if (event == null) {
skipping to change at line 92 skipping to change at line 92
this.maxLoad = maxLoad; this.maxLoad = maxLoad;
} }
@Override @Override
public void run() { public void run() {
// largest count <= maxAccessesPerTime // largest count <= maxAccessesPerTime
int maxAllowedCount = 1; int maxAllowedCount = 1;
// smallest count > maxAccessesPerTime // smallest count > maxAccessesPerTime
int minDisallowedCount = Integer.MAX_VALUE; int minDisallowedCount = Integer.MAX_VALUE;
int localMAPT = maxAccessesPerTime; int localMAPT = maxAccessesPerTime;
int totalEntries;
int clearedEntries = 0;
synchronized (numRecentAccesses) { synchronized (numRecentAccesses) {
totalEntries = numRecentAccesses.size();
Iterator<Map.Entry<String,AtomicInteger>> accessIt = numRecentAccesses.e ntrySet().iterator(); Iterator<Map.Entry<String,AtomicInteger>> accessIt = numRecentAccesses.e ntrySet().iterator();
while (accessIt.hasNext()) { while (accessIt.hasNext()) {
Map.Entry<String,AtomicInteger> entry = accessIt.next(); Map.Entry<String,AtomicInteger> entry = accessIt.next();
AtomicInteger atomicCount = entry.getValue(); AtomicInteger atomicCount = entry.getValue();
int count = atomicCount.get(); int count = atomicCount.get();
// If number of accesses is below the threshold, remove it entirely // If number of accesses is below the threshold, remove it entirely
if (count <= localMAPT) { if (count <= localMAPT) {
accessIt.remove(); accessIt.remove();
maxAllowedCount = Math.max(maxAllowedCount, count); maxAllowedCount = Math.max(maxAllowedCount, count);
clearedEntries++;
} else { } else {
// Else it exceeded the max, so log it (again)
log.warning(name + ": Blocking " + entry.getKey() + " (" + count + "
outstanding)");
// Reduce count of accesses held against the host // Reduce count of accesses held against the host
atomicCount.getAndAdd(-localMAPT); atomicCount.getAndAdd(-localMAPT);
minDisallowedCount = Math.min(minDisallowedCount, count); minDisallowedCount = Math.min(minDisallowedCount, count);
} }
} }
} }
log.info(name + ": " + clearedEntries + " of " + totalEntries + " cleared"
);
if (maxLoad != null) { if (maxLoad != null) {
OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBea n(); OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBea n();
if (mxBean == null) { if (mxBean == null) {
log.warning("Could not obtain OperatingSystemMXBean; ignoring load"); log.warning("Could not obtain OperatingSystemMXBean; ignoring load");
} else { } else {
double loadAvg = mxBean.getSystemLoadAverage(); double loadAvg = mxBean.getSystemLoadAverage();
if (loadAvg >= 0.0) { if (loadAvg >= 0.0) {
int cores = mxBean.getAvailableProcessors(); int cores = mxBean.getAvailableProcessors();
double loadRatio = loadAvg / cores; double loadRatio = loadAvg / cores;
log.info(name + ": Load ratio: " + loadRatio + " (" + loadAvg + '/' int newMaxAccessesPerTime = loadRatio > maxLoad ?
+ cores + ") vs " + maxLoad); Math.min(maxAllowedCount, Math.max(1, maxAccessesPerTime - 1)) :
maxAccessesPerTime = loadRatio > maxLoad ? Math.max(minDisallowedCount, maxAccessesPerTime);
Math.min(maxAllowedCount, maxAccessesPerTime) : log.info(name + ": Load ratio: " + loadRatio +
Math.max(minDisallowedCount, maxAccessesPerTime); " (" + loadAvg + '/' + cores + ") vs " + maxLoad +
log.info(name + ": New maxAccessesPerTime: " + maxAccessesPerTime); " ; new maxAccessesPerTime: " + newMaxAccessesPerTime);
maxAccessesPerTime = newMaxAccessesPerTime;
} }
} }
} }
} }
} }
} }
 End of changes. 7 change blocks. 
11 lines changed or deleted 16 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)