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

查看:41
本文介绍了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.

有人实施了吗?

有任何暗示吗?

推荐答案

我是 mxl/laravel-queue-rate-limit Composer 包.

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. 安装:

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

  • 此包与 Laravel 5.5+ 兼容并使用 自动发现 功能将 MichaelLedinLaravelQueueRateLimitQueueServiceProvider::class 添加到提供程序.

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

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

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

    这些设置允许在 mail 队列上每 5 秒运行 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
    

    您可以在多个队列上运行工作程序,但只有在 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天全站免登陆