PHP code example of spatie / laravel-rate-limited-job-middleware

1. Go to this page and download the library: Download spatie/laravel-rate-limited-job-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-rate-limited-job-middleware example snippets


namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Spatie\RateLimitedMiddleware\RateLimited;

class TestJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    public function handle()
    {
        // your job logic
    }

    public function middleware()
    {
        return [new RateLimited()];
    }
}

/*
 * Determine the time at which the job should timeout.
 *
 */
public function retryUntil() :  \DateTime
{
    return now()->addDay();
}

// in your job

public function middleware()
{
    $rateLimitedMiddleware = (new RateLimited())
        ->allow(30)
        ->everySeconds(60)
        ->releaseAfterSeconds(90);

    return [$rateLimitedMiddleware];
}

// in your job

/**
 * Attempt 1: Release after 60 seconds
 * Attempt 2: Release after 180 seconds
 * Attempt 3: Release after 420 seconds
 * Attempt 4: Release after 900 seconds
 */
public function middleware()
{
    $rateLimitedMiddleware = (new RateLimited())
        ->allow(30)
        ->everySeconds(60)
        ->releaseAfterOneMinute()
        ->releaseAfterBackoff($this->attempts());

    return [$rateLimitedMiddleware];
}

// in your job

/**
 * Attempt 1: Release after 5 seconds
 * Attempt 2: Release after 15 seconds
 * Attempt 3: Release after 35 seconds
 * Attempt 4: Release after 75 seconds
 */
public function middleware()
{
    $rateLimitedMiddleware = (new RateLimited())
        ->allow(30)
        ->everySeconds(60)
        ->releaseAfterSeconds(5)
        ->releaseAfterBackoff($this->attempts());

    return [$rateLimitedMiddleware];
}

// in your job

/**
 * Attempt 1: Release after 5 seconds
 * Attempt 2: Release after 20 seconds
 * Attempt 3: Release after 65 seconds
 * Attempt 4: Release after 200 seconds
 */
public function middleware()
{
    $rateLimitedMiddleware = (new RateLimited())
        ->allow(30)
        ->everySeconds(60)
        ->releaseAfterBackoff($this->attempts(), 3);

    return [$rateLimitedMiddleware];
}

public function middleware()
{
    $rateLimitedMiddleware = (new RateLimited())
        ->allow(30)
        ->everySeconds(60)
        ->dontRelease();

    return [$rateLimitedMiddleware];
}

// in your job

public function middleware()
{
    $rateLimitedMiddleware = (new RateLimited())
        ->connectionName('my-custom-connection')
        ->key('my-custom-key');

    return [$rateLimitedMiddleware];
}

// in your job

public function middleware()
{
    $shouldRateLimitJobs = Carbon::now()->month === 1;

    $rateLimitedMiddleware = (new RateLimited())
        ->enabled($shouldRateLimitJobs);

    return [$rateLimitedMiddleware];
}