Home>Article>Backend Development> What are the speed limits of Yii2.0 RESTful API? What is the use?
The content of this article is about the speed limit of Yii2.0 RESTful API? What is the use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is rate limiting?
The authoritative guide is translated as current limiting. To prevent abuse, you should consider throttling your API. For example, you can limit each user to 100 calls to the API within 10 minutes. If a large number of requests are received from a user within a specified time, response status code 429 will be returned (which means too many requests).
To enable rate limiting, you first need to implement the authentication class. I have elaborated on the authentication chapter in the Yii2.0 RESTful API Authentication Tutorial. I will not introduce it in this article. I will operate based on it again
Looking through the authoritative guide, we can see that to enable rate limit, the authentication class first needs to inherit yiifiltersRateLimitInterface
Generate two key fields
php yii migrate/create add_allowance_and_allowance_updated_at_to_user
Modify the migration file just now
/** * {@inheritdoc} */ public function safeUp() { $this->addColumn('user', 'allowance', $this->integer()); $this->addColumn('user', 'allowance_updated_at', $this->integer()); } /** * {@inheritdoc} */ public function safeDown() { $this->dropColumn('user', 'allowance'); $this->dropColumn('user', 'allowance_updated_at'); }
Execute migration
php yii migrate
Write the authentication class and inheritRateLimitInterface
namespace api\models; use Yii; use yii\base\NotSupportedException; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; use yii\filters\RateLimitInterface; use yii\web\IdentityInterface; class User extends ActiveRecord implements IdentityInterface,RateLimitInterface { . . . }
implementationRateLimitInterface
The required method
public function getRateLimit($request, $action) { return [1, 1]; // $rateLimit requests per second } public function loadAllowance($request, $action) { return [$this->allowance, $this->allowance_updated_at]; } public function saveAllowance($request, $action, $allowance, $timestamp) { $this->allowance = $allowance; $this->allowance_updated_at = $timestamp; $this->save(); }
is implemented in the controller and called
use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; use yii\filters\RateLimiter; public function behaviors() { $behaviors = parent::behaviors(); $behaviors['rateLimiter'] = [ 'class' => RateLimiter::className(), 'enableRateLimitHeaders' => true, ]; $behaviors['authenticator'] = [ 'class' => CompositeAuth::className(), 'authMethods' => [ //Http::className(), HttpBearerAuth::className(), QueryParamAuth::className(), ], ]; //$behaviors['rateLimiter']['enableRateLimitHeaders'] = true; return $behaviors; }
ok, request your action, if 429 appears in multiple requests, it means that the rate limit is enabled successfully
The above is about the use of Yii2.0 rate limit. Rate limit needs to be used in conjunction with authentication. For authentication, check out the Yii2.0 RESTful API authentication tutorial. This article recommends that you read the authentication first and complete the authentication first. function, and then enable rate limit
I think this is the end of the Yii2.0 RESTFul API. These are the core functions. The rest is specific actual combat.Practice more and type more,
The above is the detailed content of What are the speed limits of Yii2.0 RESTful API? What is the use?. For more information, please follow other related articles on the PHP Chinese website!