mxl / laravel-queue-rate-limit by mxl

Simple Laravel queue rate limiting
391,066
93
7
Package Data
Maintainer Username: mxl
Maintainer Contact: mledin89@gmail.com (Michael Ledin)
Package Create Date: 2019-08-02
Package Last Update: 2024-09-21
Language: PHP
License: MIT
Last Refreshed: 2024-12-15 15:21:54
Package Statistics
Total Downloads: 391,066
Monthly Downloads: 5,262
Daily Downloads: 18
Total Stars: 93
Total Watchers: 7
Total Forks: 19
Total Open Issues: 8

laravel-queue-rate-limit

Simple Laravel queue rate limiting

Installation

3.* versions are compatible only with Laravel 7.

$ composer require mxl/laravel-queue-rate-limit

For Laravel 6 use 2.* versions:

$ composer require mxl/laravel-queue-rate-limit "^2.0"

For Laravel 5 use 1.* versions:

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

Laravel 5.5+ will use the auto-discovery feature to add MichaelLedin\LaravelQueueRateLimit\QueueServiceProvider::class to providers.

This package is not compatible with older Laravel versions.

Add rate limits to config/queue.php:

'rateLimits' => [
     'mail' => [ // queue name
        'allows' => 1, // 1 job
        'every' => 5 // per 5 seconds
     ]
]

Usage

Make sure that you don't use sync connection when queueing jobs. See default property in config/queue.php.

Run queue worker:

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

Then push several jobs to default and mail queues:

Mail::queue(..., 'mail');
Mail::queue(..., 'mail');
Mail::queue(..., 'mail');
Mail::queue(..., 'default');
Mail::queue(..., 'default');

You'll see that only mail queue jobs will be rate limited while default queue jobs will run normally.

Disable logging

Extend QueueServiceProvider:

<?php

namespace App\Providers;

class QueueServiceProvider extends \MichaelLedin\LaravelQueueRateLimit\QueueServiceProvider
{
    protected function registerLogger()
    {
        $this->app->singleton('queue.logger', function () {
            return null;
        });
    }
}

Add it to providers array in config/app.php:

<?php

return [
    // ...
    'providers' => [
        // Laravel Framework Service Providers
        // ...
        // Application Service Providers
        // ...
        App\Providers\QueueServiceProvider::class,
        // ...
    ]
];

Maintainers

License

See the LICENSE file for details.