0

For authorized users I set limit for the frequency of queries to the system. If the number of requests per given time interval more than set, it returns a response with status code 429. In my particular case it is no more than one request in five seconds.

In identity class I implemented the yii\filters\RateLimitInterface:

<?php
namespace common\models;

use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\filters\RateLimitInterface;

/**
 * User model
 *
 * ...
 * @property integer $id
 * ...
 *
 * @property integer $allowance
 * @property integer $allowance_updated_at
  */
class User extends ActiveRecord implements IdentityInterface, RateLimitInterface
{
    ...

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * Returns the maximum number of allowed requests and the window size.
     * @param \yii\web\Request $request the current request
     * @param \yii\base\Action $action the action to be executed
     * @return array an array of two elements. The first element is the maximum number of allowed requests,
     * and the second element is the size of the window in seconds.
     */
    public function getRateLimit($request, $action)
    {
        return [1, 5];
    }

    /**
     * Loads the number of allowed requests and the corresponding timestamp from a persistent storage.
     * @param \yii\web\Request $request the current request
     * @param \yii\base\Action $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)
    {
        return [$this->allowance, $this->allowance_updated_at];
    }

    /**
     * Saves the number of allowed requests and the corresponding timestamp to a persistent storage.
     * @param \yii\web\Request $request the current request
     * @param \yii\base\Action $action the action to be executed
     * @param integer $allowance the number of allowed requests remaining.
     * @param integer $timestamp the current timestamp.
     */
    public function saveAllowance($request, $action, $allowance, $timestamp)
    {
        $this->allowance = $allowance;
        $this->allowance_updated_at = $timestamp;
        $this->save();
    }
    ...
}

In the behaviours I added the following:

'rateLimiter' => [
    'class' => RateLimiter::className(),
]

To the table tbl_user I added two fields:

[allowance] [int] NULL
[allowance_updated_at] [int] NULL

In the local environment all works fine and if the user made more than one request in 5 seconds, he will see the exception yii\web\TooManyRequestsHttpException and this page:

enter image description here

Ok.. Next, I created two migration files and applied them on the test server and on the production server:

...
yii migrate/create add_allowance_column_to_tbl_user --fields=allowance:integer
yii migrate/create add_allowance_updated_at_column_to_tbl_user --fields=allowance_updated_at:integer

yii migrate
...

Migration files:

<?php

use yii\db\Migration;

class m160722_073856_add_allowance_column_to_tbl_user extends Migration
{
    public function up()
    {
        $this->addColumn('tbl_user', 'allowance', $this->integer());
    }

    public function down()
    {
        $this->dropColumn('tbl_user', 'allowance');
    }
}

...

<?php

use yii\db\Migration;

class m160722_073932_add_allowance_updated_at_column_to_tbl_user extends Migration
{
    public function up()
    {
        $this->addColumn('tbl_user', 'allowance_updated_at', $this->integer());
    }

    public function down()
    {
        $this->dropColumn('tbl_user', 'allowance_updated_at');
    }
}

On the test server everything works perfectly.

Next, I moved the solution to the production server and have a problem:

2016-07-22 12:17:37 [10.1.80.59][25112][snadvdlp3cb15fs9kc0j3dk5c3][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: common\models\User::allowance' in D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\Component.php:143
Stack trace:
#0 D:\wwwroot\aisnew\vendor\yiisoft\yii2\db\BaseActiveRecord.php(247): yii\base\Component->__get('allowance')
#1 D:\wwwroot\aisnew\common\models\User.php(388): yii\db\BaseActiveRecord->__get('allowance')
#2 D:\wwwroot\aisnew\vendor\yiisoft\yii2\filters\RateLimiter.php(100): common\models\User->loadAllowance(Object(yii\web\Request), Object(yii\base\InlineAction))
#3 D:\wwwroot\aisnew\vendor\yiisoft\yii2\filters\RateLimiter.php(78): yii\filters\RateLimiter->checkRateLimit(Object(common\models\User), Object(yii\web\Request), Object(yii\web\Response), Object(yii\base\InlineAction))
#4 D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\ActionFilter.php(70): yii\filters\RateLimiter->beforeAction(Object(yii\base\InlineAction))
#5 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#6 D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\Component.php(541): call_user_func(Array, Object(yii\base\ActionEvent))
#7 D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\Controller.php(269): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#8 D:\wwwroot\aisnew\vendor\yiisoft\yii2\web\Controller.php(121): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))
#9 D:\wwwroot\aisnew\common\components\AisUniversityController.php(22): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#10 D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\Controller.php(152): common\components\AisUniversityController->beforeAction(Object(yii\base\InlineAction))
#11 D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('readyoperator', Array)
#12 D:\wwwroot\aisnew\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('queue/queue/rea...', Array)
#13 D:\wwwroot\aisnew\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#14 D:\wwwroot\aisnew\backend\web\index.php(18): yii\base\Application->run()

I switch the local/ test environment to the industrial database and everything works..

What can be the reason?

  • Do you run migration successful on production server? – ThanhPV Jul 22 '16 at 10:27
  • I saw that problem model don't have property in your migration. I think you should check structure table in database for property `User::allowance`. – ThanhPV Jul 22 '16 at 10:46

0 Answers0