RateLimiter
API reference › yii › filters › RateLimiter
RateLimiter implements a rate limiting algorithm based on the leaky bucket algorithm.
You may use RateLimiter by attaching it as a behavior to a controller or module, like the following,
public function behaviors(){ return [ 'rateLimiter' => [ 'class' => \yii\filters\RateLimiter::class, ], ];}When the user has exceeded his rate limit, RateLimiter will throw a TooManyRequestsHttpException exception.
Note that RateLimiter requires $user to implement the RateLimitInterface. RateLimiter will
do nothing if $user is not set or does not implement RateLimitInterface.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$enableRateLimitHeaders | bool | Whether to include rate limit headers in the response Default: true. | RateLimiter |
$errorMessage | string | The message to be displayed when rate limit exceeds Default: 'Rate limit exceeded.'. | RateLimiter |
$except | array | List of action IDs that this filter should not apply to. Default: []. | yii\ |
$only | array | List of action IDs that this filter should apply to. Default: []. | yii\ |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
$request | yii\| | The current request. Default: null. | RateLimiter |
$response | yii\| | The response to be sent. Default: null. | RateLimiter |
$user | RateLimitInterface|yii\|Closure| | The user object that implements the RateLimitInterface. Default: null. | RateLimiter |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
addRateLimitHeaders() | Adds the rate limit headers to the response. | RateLimiter |
afterAction() | This method is invoked right after an action is executed. | yii\ |
afterFilter() | yii\ | |
attach() | Attaches the behavior object to the component. | yii\ |
beforeAction() | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | RateLimiter |
beforeFilter() | yii\ | |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
checkRateLimit() | Checks whether the rate limit exceeds. | RateLimiter |
className() static | Returns the fully qualified name of this class. | yii\ |
detach() | Detaches the behavior object from the component. | yii\ |
events() | Declares event handlers for the $owner’s events. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | RateLimiter |
getActionId() protected | Returns an action ID by converting Action::$uniqueId into an ID relative to the module. | yii\ |
isActive() protected | Returns a value indicating whether the filter is active for the given action. | yii\ |
Property details
Section titled “Property details”$request
Section titled “$request ”public yii\web\Request|null $request = nullThe current request.
If not set, the request application component will be used.
$response
Section titled “$response ”public yii\web\Response|null $response = nullThe response to be sent.
If not set, the response application component will be used.
$user
Section titled “$user ”public yii\filters\RateLimitInterface|yii\web\IdentityInterface|Closure|null $user = nullThe user object that implements the RateLimitInterface.
If not set, it will take the value of Yii::$app->user->getIdentity(false).
{@since 2.0.38} It’s possible to provide a closure function in order to assign the user identity on runtime. Using a closure to assign the user identity is recommend
when you are not using the standard Yii::$app->user component. See the example below:
'user' => function() { return Yii::$app->apiUser->identity;}Method details
Section titled “Method details”addRateLimitHeaders()
Section titled “addRateLimitHeaders() ”public function addRateLimitHeaders(yii\web\Response $response, int $limit, int $remaining, int $reset): mixedAdds the rate limit headers to the response.
Parameters
| Parameter | Type | Description |
|---|---|---|
$response | yii\ | |
$limit | int | The maximum number of allowed requests during a period |
$remaining | int | The remaining number of allowed requests within the current period |
$reset | int | The number of seconds to wait before having maximum number of allowed requests again |
Returns
mixed
beforeAction()
Section titled “beforeAction() ”public function beforeAction(mixed $action): boolThis method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | mixed | The action to be executed. |
Returns
bool: Whether the action should continue to be executed.
checkRateLimit()
Section titled “checkRateLimit() ”public function checkRateLimit(yii\filters\RateLimitInterface $user, yii\web\Request $request, yii\web\Response $response, yii\base\Action $action): mixedChecks whether the rate limit exceeds.
Parameters
| Parameter | Type | Description |
|---|---|---|
$user | RateLimitInterface | The current user |
$request | yii\ | |
$response | yii\ | |
$action | yii\ | The action to be executed |
Returns
mixed
Throws
yii\web\TooManyRequestsHttpException: if rate limit exceeds
init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed