Download the PHP package spatie/laravel-rate-limited-job-middleware without Composer

On this page you can find all versions of the php package spatie/laravel-rate-limited-job-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-rate-limited-job-middleware

A job middleware to rate limit jobs

Latest Version on Packagist run-tests Total Downloads

This package contains a job middleware that can rate limit jobs in Laravel apps.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

Usage

By default, the middleware will only allow 5 jobs to be executed per second. Any jobs that are not allowed will be released for 5 seconds.

To apply the middleware just add the Spatie\RateLimitedMiddleware\RateLimited to the middlewares of your job.

Configuring attempts

When using rate limiting, the number of attempts of your job may be hard to predict. Instead of using a fixed number of attempts, it's better to use time based attempts.

You can add this to your job class:

Customizing the behaviour

You can customize all the behaviour. Here's an example where the middleware allows a maximum of 30 jobs to performed in a timespan of 60 seconds. Jobs that are not allowed will be released for 90 seconds.

Implementing Exponential Backoff

Often remote services such as APIs have rate limits or otherwise respond with a server error. Under these circumstances it makes sense to increment our delay before trying again. You can replace releaseAfter methods with releaseAfterBackoff($this->attempts() to use the default Rate Limiter interval of 5 seconds. Otherwise, you may chain the releaseAfter calls to adjust the backoff interval.

Example: releaseAfterOneMinute()

Example: releaseAfterSeconds()

Example: Customize Backoff Rate

releaseAfterBackoff() accepts the rate multiplier as the second argument. By default, the multiplier is 2.

Below is an example of setting the rate to 3. You'll notice that as the attempts grow, the difference between a rate of 2 vs. a rate of 3 becomes significantly greater.

Not releasing jobs

If you don't want to retry a job when it is ratelimited, you can use the dontRelease() method. This is useful in situations where you have jobs that run periodically and you don't care about a job being skipped.

Customizing Redis

By default, the middleware will use the default Redis connection.

The default key that will be used in redis will be the name of the class that created the instance of the middleware. In most cases this will be name of job in which the middleware is applied. If this is not what you expect, you can use the key method to customize it.

Here's an example where a custom connection and custom key is used.

Conditionally applying the middleware

If you want to conditionally apply the middleware you can use the enabled method. If accepts a boolean that determines if the middleware should rate limit your job or not.

You can also pass a Closure to enabled. If it evaluates to a truthy value the middleware will be enable.

Here's a silly example where the rate limiting is only activated in January.

Available methods.

These methods are available to be called on the middleware. Their names should be self-explanatory.

Available events.

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

This code is heavily based on the rate limiting example found in the Laravel docs.

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-rate-limited-job-middleware with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
artisansdk/ratelimiter Version ^1.1
illuminate/cache Version ^8.0|^9.0|^10.0|^11.0
illuminate/redis Version ^8.0|^9.0|^10.0|^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/laravel-rate-limited-job-middleware contains the following files

Loading the files please wait ....