Skip to content

Commit 1c13ef8

Browse files
committed
v1.0.0
Update docs
1 parent e5589b2 commit 1c13ef8

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
indent_style = space
9+
10+
[*.json]
11+
indent_size = 4
12+
13+
[*.md]
14+
trim_trailing_whitespace = false
15+
indent_size = 2

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.DS_Store
4+
/.idea

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
- 7.3
8+
9+
before_script:
10+
- travis_retry composer self-update
11+
- travis_retry composer install --prefer-source --no-interaction --dev
12+
13+
script: ./vendor/phpunit/phpunit/phpunit

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Michael Ledin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
11
# laravel-queue-rate-limit
22
Simple Laravel queue rate limiting
3+
4+
## Installation
5+
```bash
6+
$ composer require mxl/laravel-queue-rate-limit:^1.0
7+
```
8+
9+
Laravel 5.5+ will use the [auto-discovery](https://medium.com/@taylorotwell/package-auto-discovery-in-laravel-5-5-ea9e3ab20518) feature to add `MichaelLedin\LaravelQueueRateLimit\QueueServiceProvider::class` to providers.
10+
11+
This package is not compatible with older Laravel versions.
12+
13+
Add rate limits to `config/queue.php`:
14+
15+
```php
16+
'rateLimits' => [
17+
'mail' => [ // queue name
18+
'allows' => 1, // 1 job
19+
'every' => 5 // per 5 seconds
20+
]
21+
]
22+
```
23+
24+
## Usage
25+
26+
Make sure that you don't use `sync` connection when queueing jobs. See `default` property in `config/queue.php`.
27+
28+
Run queue worker:
29+
30+
```bash
31+
$ php artisan queue:work --queue default,mail
32+
```
33+
34+
Then push several jobs to `default` and `mail` queues:
35+
36+
```php
37+
Mail::queue(..., 'mail');
38+
Mail::queue(..., 'mail');
39+
Mail::queue(..., 'mail');
40+
Mail::queue(..., 'default');
41+
Mail::queue(..., 'default');
42+
```
43+
44+
You'll see that only `mail` queue jobs will be rate limited while `default` queue jobs will run normally.
45+
46+
## Maintainers
47+
48+
- [@mxl](https://github.com/mxl)
49+
50+
## License
51+
52+
See the [LICENSE](https://github.com/mxl/laravel-queue-rate-limit/blob/master/LICENSE) file for details.
53+
54+

composer.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "mxl/laravel-queue-rate-limit",
3+
"description": "Simple Laravel queue rate limiting",
4+
"keywords": [
5+
"queue",
6+
"rate",
7+
"limit",
8+
"laravel"
9+
],
10+
"license": "MIT",
11+
"homepage": "https://github.com/mxl/laravel-queue-rate-limit",
12+
"support": {
13+
"issues": "https://github.com/mxl/laravel-queue-rate-limit/issues",
14+
"source": "https://github.com/mxl/laravel-queue-rate-limit"
15+
},
16+
"type": "library",
17+
"require": {
18+
"php": ">=7.0",
19+
"illuminate/queue": "^5.5",
20+
"illuminate/contracts": "^5.5",
21+
"illuminate/cache": "^5.5",
22+
"illuminate/log": "^5.5"
23+
},
24+
"authors": [
25+
{
26+
"name": "Michael Ledin",
27+
"email": "mledin89@gmail.com"
28+
}
29+
],
30+
"autoload": {
31+
"psr-4": {
32+
"MichaelLedin\\LaravelQueueRateLimit\\": "src/"
33+
}
34+
},
35+
"extra": {
36+
"laravel": {
37+
"providers": [
38+
"MichaelLedin\\LaravelQueueRateLimit\\QueueServiceProvider"
39+
]
40+
}
41+
},
42+
"require-dev": {
43+
"phpunit/phpunit": "^7.5"
44+
},
45+
"minimum-stability": "stable"
46+
}

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
>
12+
<testsuites>
13+
<testsuite name="Package Test Suite">
14+
<directory suffix=".php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

src/QueueServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace MichaelLedin\LaravelQueueRateLimit;
4+
5+
use Illuminate\Cache\RateLimiter;
6+
use Illuminate\Contracts\Debug\ExceptionHandler;
7+
use Illuminate\Log\LogManager;
8+
9+
class QueueServiceProvider extends \Illuminate\Queue\QueueServiceProvider
10+
{
11+
protected function registerWorker()
12+
{
13+
$this->app->singleton('queue.worker', function () {
14+
return new Worker(
15+
$this->app['queue'], $this->app['events'], $this->app[ExceptionHandler::class],
16+
$this->app['config']->get('queue.rateLimits'), $this->app[RateLimiter::class], $this->app[LogManager::class]
17+
);
18+
});
19+
}
20+
}

src/Worker.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace MichaelLedin\LaravelQueueRateLimit;
4+
5+
use Illuminate\Cache\RateLimiter;
6+
use Illuminate\Contracts\Debug\ExceptionHandler;
7+
use Illuminate\Contracts\Events\Dispatcher;
8+
use Illuminate\Log\LogManager;
9+
use Illuminate\Queue\QueueManager;
10+
11+
class Worker extends \Illuminate\Queue\Worker
12+
{
13+
/**
14+
* @var array
15+
*/
16+
private $rateLimits;
17+
18+
/**
19+
* @var RateLimiter
20+
*/
21+
private $rateLimiter;
22+
23+
/**
24+
* @var LogManager
25+
*/
26+
private $logger;
27+
28+
/**
29+
* @inheritDoc
30+
* @param array|null $rateLimits
31+
* @param RateLimiter $rateLimiter
32+
* @param LogManager $logger
33+
*/
34+
public function __construct(QueueManager $manager, Dispatcher $events, ExceptionHandler $exceptions, $rateLimits, $rateLimiter, $logger)
35+
{
36+
parent::__construct($manager, $events, $exceptions);
37+
38+
$this->rateLimits = $rateLimits ?? [];
39+
$this->rateLimiter = $rateLimiter;
40+
$this->logger = $logger;
41+
}
42+
43+
protected function getNextJob($connection, $queue)
44+
{
45+
$job = null;
46+
foreach (explode(',', $queue) as $queue) {
47+
$rateLimit = $this->rateLimits[$queue] ?? null;
48+
if ($rateLimit) {
49+
if (!isset($rateLimit['allows']) || !isset($rateLimit['every'])) {
50+
throw new \RuntimeException('Set "allows" and "every" fields for "' . $queue . '" rate limit.');
51+
}
52+
$this->log('Rate limit is set for queue ' . $queue);
53+
if ($this->rateLimiter->tooManyAttempts($queue, $rateLimit['allows'])) {
54+
$availableIn = $this->rateLimiter->availableIn($queue);
55+
$this->log('Rate limit is reached for queue ' . $queue . '. Next job will be started in ' . $availableIn . ' seconds');
56+
continue;
57+
} else {
58+
$this->log('Rate limit check is passed for queue ' . $queue);
59+
}
60+
} else {
61+
$this->log('No rate limit is set for queue ' . $queue . '.');
62+
}
63+
64+
$job = parent::getNextJob($connection, $queue);
65+
if ($job) {
66+
if ($rateLimit) {
67+
$this->rateLimiter->hit($queue, $rateLimit['every']);
68+
}
69+
$this->log('Running job ' . $job->getJobId() . ' on queue ' . $queue);
70+
break;
71+
} else {
72+
$this->log('No available jobs on queue ' . $queue);
73+
}
74+
}
75+
return $job;
76+
}
77+
78+
79+
private function log(string $message)
80+
{
81+
if ($this->logger) {
82+
$this->logger->debug($message);
83+
}
84+
}
85+
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)