Skip to content

RateLimitResult

Viames Marino edited this page Oct 19, 2026 · 1 revision

Pair framework: RateLimitResult

Pair\Api\RateLimitResult describes one decision returned by RateLimiter::attempt(). It does not perform a storage check itself.

Public fields

Field Type Meaning
allowed bool Whether this request may continue.
limit int Maximum hits allowed in the window.
remaining int Hits still available after the decision.
resetAt int Unix timestamp when the current window fully resets.
retryAfter int Non-negative number of seconds before retrying a blocked request.
driver string Storage backend that produced the result, normally file or redis.

applyHeaders(): void

Emits X-RateLimit-Limit, X-RateLimit-Remaining, and X-RateLimit-Reset. A blocked result also emits Retry-After. Call this before sending a response body.

use Pair\Api\ApiResponse;
use Pair\Api\RateLimiter;

$limiter = new RateLimiter(10, 60);
$result = $limiter->attempt('export:user:' . $userId);
$result->applyHeaders();

if (!$result->allowed) {
	return ApiResponse::errorResponse('TOO_MANY_REQUESTS', [
		'retryAfter' => $result->retryAfter,
		'resetAt' => $result->resetAt,
	]);
}

applyHeaders() emits metadata only; the caller must stop work and return a response when allowed is false. ThrottleMiddleware already performs both steps for normal API requests.

See also: RateLimiter, ThrottleMiddleware, AuthAttemptLimiter, ApiErrorResponse.

Clone this wiki locally