Thread-safe per-key attempt limiter used to throttle abusive requests.
More...
#include <ratelimiter.h>
|
| bool | recordAttempt (const QString &key, int maxAttempts, int windowSeconds) |
| | Record an attempt for key and report whether the key is now blocked.
|
| bool | recordAttemptAt (const QString &key, int maxAttempts, int windowSeconds, qint64 now) |
| | Record an attempt as seen at epoch second now.
|
| bool | isBlocked (const QString &key, int maxAttempts, int windowSeconds) const |
| | True if key already has at least maxAttempts attempts within windowSeconds.
|
| bool | isBlockedAt (const QString &key, int maxAttempts, int windowSeconds, qint64 now) const |
| | Whether key is blocked as measured at epoch second now.
|
| void | clearAttempts (const QString &key) |
| | Drop all recorded attempts for the key, e.g. after a successful login.
|
| int | retryAfterSeconds (const QString &key, int maxAttempts, int windowSeconds) const |
| | Get the retry-after window in seconds for key.
|
| int | retryAfterSecondsAt (const QString &key, int maxAttempts, int windowSeconds, qint64 now) const |
| | Get the retry-after window in seconds for key as measured at now.
|
|
| void | pruneLocked (qint64 now) |
| | Drops keys whose last attempts have fully aged out of their window.
|
Thread-safe per-key attempt limiter used to throttle abusive requests.
◆ clearAttempts()
| void RateLimiter::clearAttempts |
( |
const QString & | key | ) |
|
Drop all recorded attempts for the key, e.g. after a successful login.
◆ isBlocked()
| bool RateLimiter::isBlocked |
( |
const QString & | key, |
|
|
int | maxAttempts, |
|
|
int | windowSeconds ) const |
True if key already has at least maxAttempts attempts within windowSeconds.
Reads only; records nothing. Returns false when maxAttempts <= 0 or windowSeconds <= 0. Delegates to isBlockedAt().
◆ isBlockedAt()
| bool RateLimiter::isBlockedAt |
( |
const QString & | key, |
|
|
int | maxAttempts, |
|
|
int | windowSeconds, |
|
|
qint64 | now ) const |
Whether key is blocked as measured at epoch second now.
Exposed for deterministic tests; production callers should use isBlocked().
◆ pruneLocked()
| void RateLimiter::pruneLocked |
( |
qint64 | now | ) |
|
|
private |
Drops keys whose last attempts have fully aged out of their window.
Runs at most every PruneIntervalSeconds to stop the map from growing without bound when attackers rotate source addresses.
◆ recordAttempt()
| bool RateLimiter::recordAttempt |
( |
const QString & | key, |
|
|
int | maxAttempts, |
|
|
int | windowSeconds ) |
Record an attempt for key and report whether the key is now blocked.
A key that is already at its limit is not extended: the attempt is rejected without appending, so the lockout lifts once the recorded attempts age out of the window. Returns false immediately when maxAttempts <= 0 or windowSeconds <= 0 (rate limiting disabled), recording nothing.
Delegates to recordAttemptAt() with the current wall-clock time.
◆ recordAttemptAt()
| bool RateLimiter::recordAttemptAt |
( |
const QString & | key, |
|
|
int | maxAttempts, |
|
|
int | windowSeconds, |
|
|
qint64 | now ) |
Record an attempt as seen at epoch second now.
Exposed for deterministic tests; production callers should use recordAttempt().
◆ retryAfterSeconds()
| int RateLimiter::retryAfterSeconds |
( |
const QString & | key, |
|
|
int | maxAttempts, |
|
|
int | windowSeconds ) const |
Get the retry-after window in seconds for key.
Returns the number of seconds from now until the oldest attempt exits windowSeconds and the key is no longer blocked. If the key is not blocked, returns 0. Returns 0 if maxAttempts <= 0 or windowSeconds <= 0.
Delegates to retryAfterSecondsAt().
◆ retryAfterSecondsAt()
| int RateLimiter::retryAfterSecondsAt |
( |
const QString & | key, |
|
|
int | maxAttempts, |
|
|
int | windowSeconds, |
|
|
qint64 | now ) const |
Get the retry-after window in seconds for key as measured at now.
Exposed for deterministic tests.
◆ attempts
◆ lastPruneSecs
| qint64 RateLimiter::lastPruneSecs = 0 |
|
private |
◆ mutex
| QMutex RateLimiter::mutex |
|
mutableprivate |
◆ PruneIntervalSeconds
| int RateLimiter::PruneIntervalSeconds = 60 |
|
staticconstexprprivate |
The documentation for this class was generated from the following files: