Cockatrice 2026-09-01-Development-3.1.0-beta.10
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
ServerRateLimiter Class Reference

Tracks per-server backoff state triggered by HTTP 429 responses. More...

#include <server_rate_limiter.h>

Public Member Functions

bool isRateLimited (const QString &host, const QDateTime &now) const
 Checks whether a host is currently in backoff.
int rounds (const QString &host) const
QDateTime deadline (const QString &host) const
QDateTime earliestDeadline (const QDateTime &now) const
QDateTime on429 (const QString &host, const QDateTime &now, qint64 retryAfterMs=0)
 Registers a 429 response for the given host.
void onSuccess (const QString &host)
 Clears all penalty state for a host after a successful request.
void clearExpired (const QDateTime &now)
 Removes expired backoffs, refreshing the round budget of hosts that have been idle past the reset grace period.

Static Public Attributes

static constexpr int MAX_RETRIES = 5
 Max 429 rounds per host before the budget is exhausted.
static constexpr int MIN_429_BACKOFF_MS = 30000
 Minimum wait after a 429 (scryfall documented cool-down).
static constexpr int MAX_BACKOFF_MS = 60000
 Cap for a single backoff period.
static constexpr int EXHAUSTED_BACKOFF_MS = 60000
 Cool-down once the retry budget is exhausted.
static constexpr qint64 RESET_GRACE_MS = 300000
 Idle time after which the retry budget refreshes.

Private Attributes

QMap< QString, QDateTime > backoffUntil
 When each host may be contacted again.
QMap< QString, int > retryRounds
 Consecutive 429 rounds per host.
QMap< QString, QDateTime > last429
 When each host was last rate limited.

Detailed Description

Tracks per-server backoff state triggered by HTTP 429 responses.

Keeps a monotonic backoff deadline per host and a retry-round budget that escalates once per backoff window (a burst of concurrent 429s counts as a single round). The budget is refreshed after a long period without 429s so a server is not permanently blacklisted after a past overload.

Member Function Documentation

◆ clearExpired()

void ServerRateLimiter::clearExpired ( const QDateTime & now)

Removes expired backoffs, refreshing the round budget of hosts that have been idle past the reset grace period.

Parameters
nowThe current time

◆ deadline()

QDateTime ServerRateLimiter::deadline ( const QString & host) const
nodiscard
Returns
The current backoff deadline for the host, or an invalid QDateTime.
Here is the caller graph for this function:

◆ earliestDeadline()

QDateTime ServerRateLimiter::earliestDeadline ( const QDateTime & now) const
nodiscard
Returns
The earliest active backoff deadline across all hosts, or an invalid QDateTime.

◆ isRateLimited()

bool ServerRateLimiter::isRateLimited ( const QString & host,
const QDateTime & now ) const
nodiscard

Checks whether a host is currently in backoff.

Parameters
hostThe host to check
nowThe current time
Returns
True if requests to the host should be paused

◆ on429()

QDateTime ServerRateLimiter::on429 ( const QString & host,
const QDateTime & now,
qint64 retryAfterMs = 0 )

Registers a 429 response for the given host.

Parameters
hostThe host that returned 429
nowThe time the response was received
retryAfterMsA Retry-After hint in milliseconds, or 0 if absent
Returns
The new backoff deadline for the host

The round counter only escalates once the previous backoff window has fully passed, so concurrent 429s from a burst do not consume the whole budget. The deadline is extended monotonically and never shortened.

Here is the call graph for this function:

◆ onSuccess()

void ServerRateLimiter::onSuccess ( const QString & host)

Clears all penalty state for a host after a successful request.

◆ rounds()

int ServerRateLimiter::rounds ( const QString & host) const
nodiscard
Returns
The number of consecutive 429 rounds recorded for the host.

Member Data Documentation

◆ backoffUntil

QMap<QString, QDateTime> ServerRateLimiter::backoffUntil
private

When each host may be contacted again.

◆ EXHAUSTED_BACKOFF_MS

int ServerRateLimiter::EXHAUSTED_BACKOFF_MS = 60000
staticconstexpr

Cool-down once the retry budget is exhausted.

◆ last429

QMap<QString, QDateTime> ServerRateLimiter::last429
private

When each host was last rate limited.

◆ MAX_BACKOFF_MS

int ServerRateLimiter::MAX_BACKOFF_MS = 60000
staticconstexpr

Cap for a single backoff period.

◆ MAX_RETRIES

int ServerRateLimiter::MAX_RETRIES = 5
staticconstexpr

Max 429 rounds per host before the budget is exhausted.

◆ MIN_429_BACKOFF_MS

int ServerRateLimiter::MIN_429_BACKOFF_MS = 30000
staticconstexpr

Minimum wait after a 429 (scryfall documented cool-down).

◆ RESET_GRACE_MS

qint64 ServerRateLimiter::RESET_GRACE_MS = 300000
staticconstexpr

Idle time after which the retry budget refreshes.

◆ retryRounds

QMap<QString, int> ServerRateLimiter::retryRounds
private

Consecutive 429 rounds per host.


The documentation for this class was generated from the following files: