api/components/RateLimiter.php
/**
	 * Checks whether the rate limit exceeds.
	 * @param RateLimitInterface $user the current user
	 * @param Request $request
	 * @param Response $response
	 * @param \yii\base\Action $action the action to be executed
	 * @throws TooManyRequestsHttpException if rate limit exceeds
	 */
Checks whether the rate limit exceeds.
Checks whether the rate limit exceeds .
public function checkRateLimit($user, $request, $response, $action) {
		$current = time();
		list ($limit, $window) = $user->getRateLimit($request, $action);
		list ($allowance, $timestamp) = $user->loadAllowance($request, $action);
		$allowance += (int) (($current - $timestamp) * $limit / $window);
		if ($allowance > $limit) {
			$allowance = $limit;
		}
		if ($allowance < 1) {
			$user->saveAllowance($request, $action, 0, $current);
			$this->addRateLimitHeaders($response, $limit, 0, $window);
			$params = Yii::$app->request->get();
			if(!$params){
				$params = Yii::$app->request->post();
			}
			$log = new \common\models\log\ApiV20 ([
				'client_id' => Yii::$app->user->id,
				'function' => Yii::$app->controller->action->id,
				'status' => 0,
				'cached' => 0,
				'limit' => 1,
				'ip' => ip2long(Yii::$app->request->userIP),
				'params' => null,
				'response' => null,
			]);
			$log->save();
			throw new TooManyRequestsHttpException($this->errorMessage);
		} else {
			$user->saveAllowance($request, $action, $allowance - 1, $current);
			$this->addRateLimitHeaders($response, $limit, $allowance - 1, (int) (($limit - $allowance) * $window / $limit));
		}
	}
public function checkRateLimit ( $ user , $ request , $ response , $ action ) { $ current = time ( ) ; list ( $ limit , $ window ) = $ user -> getRateLimit ( $ request , $ action ) ; list ( $ allowance , $ timestamp ) = $ user -> loadAllowance ( $ request , $ action ) ; $ allowance += ( int ) ( ( $ current - $ timestamp ) * $ limit / $ window ) ; if ( $ allowance > $ limit ) { $ allowance = $ limit ; } if ( $ allowance < 1 ) { $ user -> saveAllowance ( $ request , $ action , 0 , $ current ) ; $ this -> addRateLimitHeaders ( $ response , $ limit , 0 , $ window ) ; $ params = Yii :: $ app -> request -> get ( ) ; if ( ! $ params ) { $ params = Yii :: $ app -> request -> post ( ) ; } $ log = new \ common \ models \ log \ ApiV20 ( [ 'client_id' => Yii :: $ app -> user -> id , 'function' => Yii :: $ app -> controller -> action -> id , 'status' => 0 , 'cached' => 0 , 'limit' => 1 , 'ip' => ip2long ( Yii :: $ app -> request -> userIP ) , 'params' => null , 'response' => null , ] ) ; $ log -> save ( ) ; throw new TooManyRequestsHttpException ( $ this -> errorMessage ) ; } else { $ user -> saveAllowance ( $ request , $ action , $ allowance - 1 , $ current ) ; $ this -> addRateLimitHeaders ( $ response , $ limit , $ allowance - 1 , ( int ) ( ( $ limit - $ allowance ) * $ window / $ limit ) ) ; } }
Checks whether the rate limit exceeds.
Checks whether the rate limit exceeds .