public function checkRateLimit($user, $request, $response, $action)
    {
        [$limit, $window] = $user->getRateLimit($request, $action);
        [$allowance, $timestamp] = $user->loadAllowance($request, $action);

        $current = time();

        $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);
        }

        $user->saveAllowance($request, $action, $allowance - 1, $current);
        $this->addRateLimitHeaders($response, $limit, $allowance - 1, (int) (($limit - $allowance + 1) * $window / $limit));
    }
public function checkRateLimit($user, $request, $response, $action)
    {
        [$limit, $window] = $user->getRateLimit($request, $action);
        [$allowance, $timestamp] = $user->loadAllowance($request, $action);

        $current = time();

        $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);
        }

        $user->saveAllowance($request, $action, $allowance - 1, $current);
        $this->addRateLimitHeaders($response, $limit, $allowance - 1, (int) (($limit - $allowance + 1) * $window / $limit));
    }
public function checkRateLimit ( $ user , $ request , $ response , $ action ) { [ $ limit , $ window ] = $ user -> getRateLimit ( $ request , $ action ) ; [ $ allowance , $ timestamp ] = $ user -> loadAllowance ( $ request , $ action ) ; $ current = time ( ) ; $ 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 ) ; } $ user -> saveAllowance ( $ request , $ action , $ allowance - 1 , $ current ) ; $ this -> addRateLimitHeaders ( $ response , $ limit , $ allowance - 1 , ( int ) ( ( $ limit - $ allowance + 1 ) * $ window / $ limit ) ) ; }
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 .