RateLimiter
in package
Table of Contents
Properties
- $lockDir : string
- $store : RateLimitStoreInterface
- $waitMode : bool
Methods
- __construct() : mixed
- Create a RateLimiter configured with a storage backend and a handling mode for exceeded limits.
- acquireLock() : resource
- Acquire an exclusive, cross-process lock for the given client so that concurrent processes sharing the same client (e.g. the same certificate) serialize their check-send-update cycle instead of racing past each other's stale counters.
- checkBeforeRequest() : void
- Ensures the client is allowed to make the next request by enforcing per-second and per-day rate limits.
- handleRateLimits() : void
- Stores remaining rate-limit counts for a client for both the one-second and 24-hour windows.
- isWaitMode() : bool
- Indicates whether the limiter is configured to wait when a rate limit is exceeded.
- releaseLock() : void
- Release a lock previously acquired with {@see acquireLock()}.
Properties
$lockDir
private
string
$lockDir
$store
private
RateLimitStoreInterface
$store
$waitMode
private
bool
$waitMode
Methods
__construct()
Create a RateLimiter configured with a storage backend and a handling mode for exceeded limits.
public
__construct(RateLimitStoreInterface $store[, bool $waitMode = true ][, string $lockDir = null ]) : mixed
Parameters
- $store : RateLimitStoreInterface
-
storage backend for per-client rate-limit state
- $waitMode : bool = true
-
if true, the limiter will wait until the limit window resets; if false, it will throw a RateLimitExceededException when limits are exceeded
- $lockDir : string = null
-
directory used to store per-client lock files that serialize concurrent requests (defaults to the system temp directory)
acquireLock()
Acquire an exclusive, cross-process lock for the given client so that concurrent processes sharing the same client (e.g. the same certificate) serialize their check-send-update cycle instead of racing past each other's stale counters.
public
acquireLock(string $clientId) : resource
The lock is released with releaseLock(); it is also released automatically by the OS if the process dies while holding it.
Parameters
- $clientId : string
-
identifier of the client to lock (used to derive the lock file name)
Return values
resource —the open, locked file handle to pass to releaseLock()
checkBeforeRequest()
Ensures the client is allowed to make the next request by enforcing per-second and per-day rate limits.
public
checkBeforeRequest(string $clientId) : void
If a window is exhausted and wait mode is enabled, pauses execution for the required seconds to clear the window; otherwise throws a RateLimitExceededException.
Parameters
- $clientId : string
-
identifier of the client whose rate limits are checked
Tags
handleRateLimits()
Stores remaining rate-limit counts for a client for both the one-second and 24-hour windows.
public
handleRateLimits(string $clientId, int $remainingSecond, int $remainingDay, int $timestamp) : void
Parameters
- $clientId : string
-
Fingerprint identifying the client (e.g., certificate SHA1, serial+issuer).
- $remainingSecond : int
-
remaining requests in the current one-second window
- $remainingDay : int
-
remaining requests in the current 24-hour window
- $timestamp : int
-
UNIX timestamp (seconds) when the limits were observed
isWaitMode()
Indicates whether the limiter is configured to wait when a rate limit is exceeded.
public
isWaitMode() : bool
Return values
bool —true if the limiter waits until the rate-limit window expires, false otherwise
releaseLock()
Release a lock previously acquired with {@see acquireLock()}.
public
releaseLock(resource $handle) : void
Parameters
- $handle : resource
-
the file handle returned by acquireLock()