【问题标题】:Why Laravel Redis::throttle is so slow when could not obtain lock?为什么 Laravel Redis::throttle 无法获得锁时那么慢?
【发布时间】:2019-07-29 09:38:48
【问题描述】:

我正在尝试像documentation 中所说的那样限制繁重的操作。当允许操作时 - 它会以极快的速度开火。

但是当操作达到油门限制时 - 它会等待大约 3 秒,然后再触发油门回调。代码如下:

Artisan::command('temp', function () {
    Illuminate\Support\Facades\Log::debug("Start temp");
    Illuminate\Support\Facades\Redis::throttle("test123")
        ->allow(1)
        ->every(5)
        ->then(function () {
            Illuminate\Support\Facades\Log::debug("Logged immediately when allowed");
        }, function () {
            Illuminate\Support\Facades\Log::debug("Logged after ~3 seconds on throttle");
        });

        Illuminate\Support\Facades\Log::debug("Logged immediately or after ~3 seconds depending on above");
});

我预计on throttle 回调也会立即触发。有办法解决吗?

【问题讨论】:

    标签: laravel redis throttling


    【解决方案1】:

    当我面临同样的问题时,我明白了为什么需要这么长时间 - 看着Illuminate\Redis\Limiters\DurationLimiterBuilder,我们看到了:

    /**
     * The amount of time to block until a lock is available.
     *
     * @var int
     */
    public $timeout = 3;
    

    ...

    /**
     * Set the amount of time to block until a lock is available.
     *
     * @param  int  $timeout
     * @return $this
     */
    public function block($timeout)
    {
        $this->timeout = $timeout;
    
        return $this;
    }
    

    所以理论上你只需要添加 ->block(1) 或者如果你正在使用作业并且你延迟重新排队它们,你也可以使用 0。

    (实际上我必须清除缓存并执行composer dump-autoload 才能使其工作)

    此外,如果您使用redis-cli monitorcommand,您会看到缓存中发生了什么,非常适合调试。

    编辑: 过了一会儿我改成了Redis::funnel,你也可以看看

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2019-03-27
      • 2014-07-27
      • 2016-07-08
      相关资源
      最近更新 更多