Laravel队列速率限制或限制 [英] Laravel queue rate limiting or throttling

查看:836
本文介绍了Laravel队列速率限制或限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要从第三方服务器获取数据并且该服务器每秒最多允许1个请求的应用程序.

I am working on an app that requires fetching data from a third-party server and that server allows max 1 request per seconds.

现在,所有请求都作为作业发送,我正在尝试实施Laravel"汇率限制",以每秒释放1个作业,但无法弄清楚为什么应该实施该作业,并且网络上没有实际的示例.

Now, all request send as job and I am trying to implement Laravel "Rate Limiting" to release 1 job per second but unable to figure out why it should be implemented and there is no real-life example in the web.

有人实施吗?

有什么暗示吗?

推荐答案

我是

I'm the author of mxl/laravel-queue-rate-limit Composer package.

它允许您在不使用Redis的情况下为特定队列上的作业限制率.

It allows you to rate limit jobs on specific queue without using Redis.

  1. 通过以下方式安装:

  1. Install it with:

$ composer require mxl/laravel-queue-rate-limit:^1.0

  • 此软件包与Laravel 5.5+兼容,并使用

  • This package is compatible with Laravel 5.5+ and uses auto-discovery feature to add MichaelLedin\LaravelQueueRateLimit\QueueServiceProvider::class to providers.

    将速率限制设置添加到config/queue.php:

    Add rate limit settings to config/queue.php:

    'rateLimit' => [
        'mail' => [
            'allows' => 1,
            'every' => 5
        ]
    ]
    

    这些设置允许每5秒钟在mail队列上运行1个作业. 确保将默认队列驱动程序(config/queue.php中的default属性)设置为除sync之外的任何值.

    These settings allow to run 1 job every 5 seconds on mail queue. Make sure that default queue driver (default property in config/queue.php) is set to any value except sync.

    使用--queue mail选项运行队列工作器:

    Run queue worker with --queue mail option:

    $ php artisan queue:work --queue mail
    

    您可以在多个队列上运行worker,但是只有rateLimit设置中引用的队列会受到速率限制:

    You can run worker on multiple queues, but only queues referenced in rateLimit setting will be rate limited:

    $ php artisan qeueu:work --queue mail,default
    

    default队列上的作业将不受速率限制地执行.

    Jobs on default queue will be executed without rate limiting.

    排队一些作业以测试速率限制:

    Queue some jobs to test rate limiting:

    SomeJob::dispatch()->onQueue('mail');
    SomeJob::dispatch()->onQueue('mail');
    SomeJob::dispatch()->onQueue('mail');
    SomeJob::dispatch();
    

  • 这篇关于Laravel队列速率限制或限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆