Cockatrice 2026-09-01-Development-3.1.0-beta.10
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
server_rate_limiter.h
Go to the documentation of this file.
1#ifndef SERVER_RATE_LIMITER_H
2#define SERVER_RATE_LIMITER_H
3
4#include <QDateTime>
5#include <QMap>
6#include <QString>
7
19{
20public:
21 static constexpr int MAX_RETRIES = 5;
22 static constexpr int MIN_429_BACKOFF_MS = 30000;
23 static constexpr int MAX_BACKOFF_MS = 60000;
24 static constexpr int EXHAUSTED_BACKOFF_MS = 60000;
25 static constexpr qint64 RESET_GRACE_MS = 300000;
26
33 [[nodiscard]] bool isRateLimited(const QString &host, const QDateTime &now) const;
34
36 [[nodiscard]] int rounds(const QString &host) const;
37
39 [[nodiscard]] QDateTime deadline(const QString &host) const;
40
42 [[nodiscard]] QDateTime earliestDeadline(const QDateTime &now) const;
43
55 QDateTime on429(const QString &host, const QDateTime &now, qint64 retryAfterMs = 0);
56
58 void onSuccess(const QString &host);
59
65 void clearExpired(const QDateTime &now);
66
67private:
68 QMap<QString, QDateTime> backoffUntil;
69 QMap<QString, int> retryRounds;
70 QMap<QString, QDateTime> last429;
71};
72
73#endif // SERVER_RATE_LIMITER_H
Tracks per-server backoff state triggered by HTTP 429 responses.
Definition server_rate_limiter.h:19
QDateTime on429(const QString &host, const QDateTime &now, qint64 retryAfterMs=0)
Registers a 429 response for the given host.
Definition server_rate_limiter.cpp:30
QMap< QString, int > retryRounds
Consecutive 429 rounds per host.
Definition server_rate_limiter.h:69
QDateTime earliestDeadline(const QDateTime &now) const
Definition server_rate_limiter.cpp:19
QDateTime deadline(const QString &host) const
Definition server_rate_limiter.cpp:14
static constexpr int EXHAUSTED_BACKOFF_MS
Cool-down once the retry budget is exhausted.
Definition server_rate_limiter.h:24
bool isRateLimited(const QString &host, const QDateTime &now) const
Checks whether a host is currently in backoff.
Definition server_rate_limiter.cpp:3
QMap< QString, QDateTime > backoffUntil
When each host may be contacted again.
Definition server_rate_limiter.h:68
void clearExpired(const QDateTime &now)
Removes expired backoffs, refreshing the round budget of hosts that have been idle past the reset gra...
Definition server_rate_limiter.cpp:68
int rounds(const QString &host) const
Definition server_rate_limiter.cpp:9
static constexpr int MIN_429_BACKOFF_MS
Minimum wait after a 429 (scryfall documented cool-down).
Definition server_rate_limiter.h:22
static constexpr int MAX_BACKOFF_MS
Cap for a single backoff period.
Definition server_rate_limiter.h:23
QMap< QString, QDateTime > last429
When each host was last rate limited.
Definition server_rate_limiter.h:70
static constexpr qint64 RESET_GRACE_MS
Idle time after which the retry budget refreshes.
Definition server_rate_limiter.h:25
static constexpr int MAX_RETRIES
Max 429 rounds per host before the budget is exhausted.
Definition server_rate_limiter.h:21
void onSuccess(const QString &host)
Clears all penalty state for a host after a successful request.
Definition server_rate_limiter.cpp:61