You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "rmannibucau (via GitHub)" <gi...@apache.org> on 2023/05/04 06:36:14 UTC

[GitHub] [tomcat] rmannibucau commented on a diff in pull request #607: Added RateLimitFilter

rmannibucau commented on code in PR #607:
URL: https://github.com/apache/tomcat/pull/607#discussion_r1184594119


##########
java/org/apache/catalina/util/TimeBucketCounter.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.catalina.util;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * this class maintains a thread safe hash map that has timestamp-based buckets
+ * followed by a string for a key, and a counter for a value. each time the
+ * increment() method is called it adds the key if it does not exist, increments
+ * its value and returns it.
+ *
+ * a maintenance thread cleans up keys that are prefixed by previous timestamp
+ * buckets.
+ */
+public class TimeBucketCounter {
+
+    /**
+     * Map to hold the buckets
+     */
+    private final ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();
+
+    /**
+     * Milliseconds bucket size as a Power of 2 for bit shift math, e.g.
+     * 16 for 65_536ms which is about 1:05 minute
+     */
+    private final int numBits;
+
+    /**
+     * ratio of actual duration to config duration
+     */
+    private final double ratio;
+
+    /**
+     * flag for the maintenance thread
+     */
+    volatile boolean isRunning = false;
+
+    /**
+     *
+     * @param bucketDuration duration in seconds, e.g. for 1 minute pass 60
+     */
+    public TimeBucketCounter(int bucketDuration) {
+
+        int durationMillis = bucketDuration * 1000;
+
+        int bits = 0;
+        int pof2 = nextPowerOf2(durationMillis);
+        int bitCheck = pof2;
+        while (bitCheck > 1) {
+            bitCheck = pof2 >> ++bits;
+        }
+
+        this.numBits = bits;
+
+        this.ratio = ratioToPowerOf2(durationMillis);
+
+        int cleanupsPerBucketDuration = (durationMillis >= 60_000) ? 6 : 3;
+        Thread mt = new MaintenanceThread(durationMillis / cleanupsPerBucketDuration);
+        mt.start();
+    }
+
+    /**
+     * increments the counter for the passed identifier in the current time
+     * bucket and returns the new value
+     *
+     * @param identifier an identifier for which we want to maintain count, e.g. IP Address
+     * @return the count within the current time bucket
+     */
+    public final int increment(String identifier) {
+        String key = getCurrentBucketPrefix() + "-" + identifier;
+        AtomicInteger ai = map.computeIfAbsent(key, v -> new AtomicInteger());
+        return ai.incrementAndGet();

Review Comment:
   Ok-ish but so should maybe be written as a limitation in the filter class javadoc cause rate limiting is way more generic than current impl (which is ok but reading the doc it is not obvious IMHO).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org