Skip to content

[UNDERTOW-2537] Implement bit shift window rate limiter and limiter h…#1777

Open
baranowb wants to merge 1 commit intoundertow-io:mainfrom
baranowb:UNDERTOW-2537
Open

[UNDERTOW-2537] Implement bit shift window rate limiter and limiter h…#1777
baranowb wants to merge 1 commit intoundertow-io:mainfrom
baranowb:UNDERTOW-2537

Conversation

@baranowb
Copy link
Copy Markdown
Contributor

@baranowb baranowb commented Sep 3, 2025

…andler
Issue: https://issues.redhat.com/browse/UNDERTOW-2537

This needs review. Initially I was going to have Long as key and use bit shifts to encode there everything, but:

  1. it tad harder to debug
  2. due to some errors I had to debug....

If needs be I can switch it to above without problem. It follows similar concept of common sliding window with low accuracy as apache does in its impl. However interface should allow to implement different stategies as well relatively easily .
TestCase is a bit of PITa as to test different aspects it has to be at least 60s( might be less) so it chugs some time on CI...

@baranowb baranowb added enhancement Enhances existing behaviour or code under verification Currently being verified (running tests, reviewing) before posting a review to contributor waiting CI check Ready to be merged but waiting for CI check new feature / API change New feature to be introduced or a change to the API (non suitable to minor releases) labels Sep 3, 2025
}
}

private String getIPAddress(final HttpServerExchange exchange, final boolean warn) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is not ideal, this code is repeated in Handler class to get log details as well.

final String current = String.valueOf(currentPrefix);
final String next = String.valueOf(currentPrefix + 1);

ConcurrentHashMap.KeySetView<String, AtomicInteger> keys = requestCounter.keySet();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

String could be turned into Long with high bits holding IP in pure form and lower working as in this case. But its harder to follow.

@baranowb baranowb force-pushed the UNDERTOW-2537 branch 3 times, most recently from 1e0c5ca to 4e10443 Compare November 28, 2025 14:29
* {@link RateLimiter} has single, common window for all entries. For which keys are computed with bit shifting ops. This is not
* precise, but has very low performance impact.
*/
public class BitShiftSingleWindowRateLimiter implements RateLimiter<HttpServerExchange> {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

RateLimiter is parametrized, though Im not sure I can now see any benefit from it? Can we just stick to HttpServerExchange and get rid of it ?
@fl4via @ropalka

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, @baranowb . But, just to better understand your pov, you have added the parameter yourself, is that correct? Or is it based on something else that had already the parameters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, I envisioned it as being flexible enough for users to limit based on their needs/input, but given that we are kind of always constrained to Exchange, I dont think we need this at this point? KISS ?
@fl4via ^

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm, on second look I need to refresh this PR in my head.

@baranowb baranowb added the waiting peer review PRs that edit core classes might require an extra review label Nov 28, 2025
/**
* Utility method to generate key from exchange.
*/
K generateKey(HttpServerExchange e);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This: #1777 (review) - is directly result of obfuscation, in order to have it agnostic in implementation. This is awkward at best ?

@baranowb baranowb force-pushed the UNDERTOW-2537 branch 2 times, most recently from 3d6c68f to cf06a3c Compare December 2, 2025 13:04
@fl4via fl4via removed the waiting CI check Ready to be merged but waiting for CI check label Feb 16, 2026
@fl4via
Copy link
Copy Markdown
Member

fl4via commented Feb 16, 2026

This is on hold for now, as we are discussing how we are going to move on with handlers, that are directly exposed in the server without going through a proper WildFly RFE process

* {@link RateLimiter} has single, common window for all entries. For which keys are computed with bit shifting ops. This is not
* precise, but has very low performance impact.
*/
public class BitShiftSingleWindowRateLimiter implements RateLimiter<HttpServerExchange> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, @baranowb . But, just to better understand your pov, you have added the parameter yourself, is that correct? Or is it based on something else that had already the parameters?

}

if (currentRequestCount > rateLimiter.getRequestLimit()) {
UndertowLogger.REQUEST_LOGGER.exchangeExceedsRequestRateLimit(exchange.getRequestURI(), ipAddress,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Printing these warning messages will cause the server log to fill up quickly in case of an attack, thus enabling a DoS. We need to log this only once and, once it hits the limit, log it at debug level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, fair enough.

@fl4via fl4via added waiting PR update Awaiting PR update(s) from contributor before merging and removed under verification Currently being verified (running tests, reviewing) before posting a review to contributor waiting peer review PRs that edit core classes might require an extra review enhancement Enhances existing behaviour or code labels Mar 4, 2026
@baranowb baranowb removed the waiting PR update Awaiting PR update(s) from contributor before merging label Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature / API change New feature to be introduced or a change to the API (non suitable to minor releases)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants