RateLimitMiddleware
in package
implements
MiddlewareInterface
Class RateLimitMiddleware
Interfaces, Classes and Traits
- MiddlewareInterface
Table of Contents
- DEFAULT_RATE_LIMIT = 5000
- Defines the maximum request limit for an authorized client.
- DEFAULT_SESSION_LIFETIME = 15 * 60
- Defines the duration of an API session in seconds.
- $apiSessionsCache : SafeCache
- $rateLimit : array<string|int, mixed>
- $responseFactory : ResponseFactoryInterface
- __construct() : mixed
- RateLimitMiddleware constructor.
- process() : ResponseInterface
- checkRateLimit() : bool
- Checks the current rate limit.
- fetchRateLimit() : void
- Fetches the current rate limit from the sessions cache.
- reduceRateLimit() : void
- Reduces the current rate limit by one and stores the session.
Constants
DEFAULT_RATE_LIMIT
Defines the maximum request limit for an authorized client.
private
mixed
DEFAULT_RATE_LIMIT
= 5000
DEFAULT_SESSION_LIFETIME
Defines the duration of an API session in seconds.
private
mixed
DEFAULT_SESSION_LIFETIME
= 15 * 60
Properties
$apiSessionsCache
private
SafeCache
$apiSessionsCache
$rateLimit
private
array<string|int, mixed>
$rateLimit
$responseFactory
private
ResponseFactoryInterface
$responseFactory
Methods
__construct()
RateLimitMiddleware constructor.
public
__construct(ResponseFactoryInterface $responseFactory, SafeCache $apiSessionsCache) : mixed
Parameters
- $responseFactory : ResponseFactoryInterface
- $apiSessionsCache : SafeCache
Return values
mixed —process()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface
Return values
ResponseInterface —checkRateLimit()
Checks the current rate limit.
private
checkRateLimit(string $sessionIdentifier) : bool
Parameters
- $sessionIdentifier : string
Return values
bool —fetchRateLimit()
Fetches the current rate limit from the sessions cache.
private
fetchRateLimit(string $sessionIdentifier) : void
Parameters
- $sessionIdentifier : string
Return values
void —reduceRateLimit()
Reduces the current rate limit by one and stores the session.
private
reduceRateLimit(string $sessionIdentifier) : void
Parameters
- $sessionIdentifier : string