yii\filters\RateLimitInterface::loadAllowance PHP Method

loadAllowance() public method

Loads the number of allowed requests and the corresponding timestamp from a persistent storage.
public loadAllowance ( Request $request, Action $action ) : array
$request yii\web\Request the current request
$action yii\base\Action the action to be executed
return array an array of two elements. The first element is the number of allowed requests, and the second element is the corresponding UNIX timestamp.
    public function loadAllowance($request, $action);

Usage Example

コード例 #1
0
ファイル: RateLimiter.php プロジェクト: yiisoft/yii2
 /**
  * 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
  */
 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);
         throw new TooManyRequestsHttpException($this->errorMessage);
     } else {
         $user->saveAllowance($request, $action, $allowance - 1, $current);
         $this->addRateLimitHeaders($response, $limit, $allowance - 1, (int) (($limit - $allowance) * $window / $limit));
     }
 }