Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8067 Use nanotime for DosFilter rate tracker #8082

Merged
merged 3 commits into from Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -326,7 +326,7 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response
tracker = getRateTracker(request);

// Calculate the rate and check if it is over the allowed limit
final OverLimit overLimit = tracker.isRateExceeded(System.currentTimeMillis());
final OverLimit overLimit = tracker.isRateExceeded(System.nanoTime());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the javadoc of isRateExceeded. It specifies that the value is in ms.

now the time now (in milliseconds)


// Pass it through if we are not currently over the rate limit.
if (overLimit == null)
Expand Down Expand Up @@ -1216,8 +1216,8 @@ public RateTracker(ServletContext context, String filterName, String id, RateTyp
}

/**
* @param now the time now (in milliseconds)
* @return the current calculated request rate over the last second
* @param now the time now (in nanoseconds) used to calculate elapsed time since previous requests.
* @return the current calculated request rate over the last second if rate exceeded, else null.
*/
public OverLimit isRateExceeded(long now)
{
Expand All @@ -1235,9 +1235,9 @@ public OverLimit isRateExceeded(long now)
}

long rate = (now - last);
if (rate < 1000L)
if (TimeUnit.NANOSECONDS.toSeconds(rate) < 1L)
{
return new Overage(Duration.ofMillis(rate), _maxRequestsPerSecond);
return new Overage(Duration.ofNanos(rate), _maxRequestsPerSecond);
}
return null;
}
Expand Down Expand Up @@ -1326,7 +1326,7 @@ public void run()

int latestIndex = _next == 0 ? (_timestamps.length - 1) : (_next - 1);
long last = _timestamps[latestIndex];
boolean hasRecentRequest = last != 0 && (System.currentTimeMillis() - last) < 1000L;
boolean hasRecentRequest = last != 0 && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - last) < 1L;

DoSFilter filter = (DoSFilter)_context.getAttribute(_filterName);

Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -179,7 +178,7 @@ private boolean hitRateTracker(DoSFilter doSFilter, int sleep) throws Interrupte
for (int i = 0; i < 5; i++)
{
Thread.sleep(sleep);
if (rateTracker.isRateExceeded(TimeUnit.NANOSECONDS.toMillis(System.nanoTime())) != null)
if (rateTracker.isRateExceeded(System.nanoTime()) != null)
exceeded = true;
}
return exceeded;
Expand Down