mirror of
https://github.com/liquidraver/ZephCore.git
synced 2026-12-06 18:04:37 +00:00
43 lines
895 B
C++
43 lines
895 B
C++
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* RateLimiter - simple rate limiting for repeaters
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
class RateLimiter {
|
|
/* Members ordered to match constructor initialization order */
|
|
uint16_t _maximum;
|
|
uint32_t _secs;
|
|
uint32_t _start_timestamp;
|
|
uint16_t _count;
|
|
|
|
public:
|
|
RateLimiter(uint16_t maximum, uint32_t secs)
|
|
: _maximum(maximum), _secs(secs), _start_timestamp(0), _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;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* The window's maximum is reached: allow() would deny. Counts nothing. */
|
|
bool blocked(uint32_t now) const {
|
|
return now < _start_timestamp + _secs && _count >= _maximum;
|
|
}
|
|
|
|
void reset() {
|
|
_start_timestamp = 0;
|
|
_count = 0;
|
|
}
|
|
};
|