-
-
Notifications
You must be signed in to change notification settings - Fork 883
WIP: Add adaptive advert flood rate limiter #2545
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
Draft
ViezeVingertjes
wants to merge
1
commit into
meshcore-dev:dev
Choose a base branch
from
ViezeVingertjes:advert-flood-protection
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,21 +3,100 @@ | |
| #include <stdint.h> | ||
|
|
||
| class RateLimiter { | ||
| uint32_t _start_timestamp; | ||
| uint32_t _secs; | ||
| uint16_t _maximum, _count; | ||
| uint32_t _start; | ||
| uint16_t _secs; | ||
| uint16_t _maximum; | ||
| uint16_t _count; | ||
|
|
||
| public: | ||
| RateLimiter(uint16_t maximum, uint32_t secs): _maximum(maximum), _secs(secs), _start_timestamp(0), _count(0) { } | ||
| RateLimiter(uint16_t maximum, uint16_t secs) | ||
| : _start(0), _secs(secs), _maximum(maximum), _count(0) {} | ||
|
|
||
| bool allow(uint32_t now) { | ||
| if (now < _start_timestamp + _secs) { | ||
| _count++; | ||
| if (_count > _maximum) return false; // deny | ||
| } else { // time window now expired | ||
| _start_timestamp = now; | ||
| _count = 1; | ||
| if (now - _start >= _secs) { | ||
| _start = now; | ||
| _count = 0; | ||
| } | ||
|
|
||
| if (_count >= _maximum) | ||
| return false; | ||
|
|
||
| _count++; | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| class AdaptiveRateLimiter { | ||
| enum { | ||
| EWMA_SMOOTHING = 3, | ||
| EWMA_TOTAL_WEIGHT = EWMA_SMOOTHING + 1, | ||
| EWMA_GROWTH_CAP = 4 | ||
| }; | ||
|
|
||
| static_assert(EWMA_SMOOTHING >= 1 && EWMA_SMOOTHING <= 256, "EWMA_SMOOTHING must be 1-256"); | ||
| static_assert(EWMA_GROWTH_CAP >= 1, "EWMA_GROWTH_CAP must be at least 1"); | ||
|
|
||
| uint32_t _start; | ||
| uint16_t _secs; | ||
| uint8_t _count; | ||
| uint8_t _limit; | ||
| uint8_t _ewma; | ||
| uint8_t _burst; | ||
| uint8_t _floor; | ||
|
|
||
| static uint8_t clampU8(uint16_t v) { return v > 255 ? 255 : (uint8_t)v; } | ||
|
|
||
| uint8_t nextEwma() const { | ||
| uint8_t cap = clampU8((uint16_t)_ewma + EWMA_GROWTH_CAP); | ||
| uint8_t effective = (_count > _ewma) ? cap : _count; | ||
| uint16_t next = (uint16_t)_ewma * EWMA_SMOOTHING + effective; | ||
|
|
||
| return (uint8_t)(next / EWMA_TOTAL_WEIGHT); | ||
| } | ||
|
|
||
| uint8_t computeLimit() const { | ||
| uint8_t clamped = clampU8((uint16_t)_ewma * _burst); | ||
| return clamped > _floor ? clamped : _floor; | ||
| } | ||
|
|
||
| void advanceWindow(uint32_t now) { | ||
| if (now - _start < _secs) | ||
| return; | ||
|
|
||
| uint32_t elapsed = (_secs == 0) ? 1 : (now - _start) / _secs; | ||
|
|
||
| if (elapsed > EWMA_TOTAL_WEIGHT * 8) | ||
| elapsed = EWMA_TOTAL_WEIGHT * 8; | ||
|
|
||
| _ewma = nextEwma(); | ||
| _limit = computeLimit(); | ||
|
|
||
| while (elapsed > 1 && _ewma > 0) { | ||
| _count = 0; | ||
| _ewma = nextEwma(); | ||
| _limit = computeLimit(); | ||
|
|
||
| elapsed--; | ||
| } | ||
|
|
||
| _start = now; | ||
| _count = 0; | ||
| } | ||
|
|
||
| public: | ||
| AdaptiveRateLimiter(uint16_t secs, uint8_t burst, uint8_t floor) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding some in-line documentation explaining what these arguments mean. |
||
| : _start(0), _secs(secs), _count(0), _limit(floor), _ewma(floor), | ||
| _burst(burst), _floor(floor) {} | ||
|
|
||
| bool allow(uint32_t now) { | ||
| advanceWindow(now); | ||
|
|
||
| if (_count >= _limit) | ||
| return false; | ||
|
|
||
| _count++; | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding some documentation explaining the approach uses Exponentially Weighted Moving Average (EWMA). It might be good to have unit tests for this class to verify its behavior.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do unit tests these days?! haha.
No, all jokes aside, that should be easy to add in this case so will definetely do.