Laravel队列速率限制或限制 [英] Laravel queue rate limiting or throttling
问题描述
我正在开发一个需要从第三方服务器获取数据并且该服务器每秒最多允许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.
-
通过以下方式安装:
Install it with:
$ composer require mxl/laravel-queue-rate-limit:^1.0
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屋!