RollingCoders's avatar

Queue RateLimiter not working

hello, i have setted up my rate limiter like this inside appServiceProvider.php

    RateLimiter::for('invite', function ($job) {
        return Limit::perMinutes(
            decayMinutes: 10,
            maxAttempts: 5
        );
    });

i have setted up my job like this

class SendInviteMailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(
        private invite $invite,
        private Contact $contact,
    ) {
        //
    }

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

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        FacadesLog::info('Sending invite mail to: ' . $this->contact->email);
        Mail::to($this->contact->email)->send($this->invite);
    }
}

but when i do queue:work all the jobs (i queue 2k jobs) are runned together, without limiting the execution, what am i doing wrong? thanks

1 like
2 replies
vincent15000's avatar

Not sure, I think that the rate limiter is applied for each job individually, I mean that if a job fails, it will be retried according to the rate limiter.

Can somebody confirm ?

Are you trying to execute the jobs by chunk ?

Niush's avatar

Like how we rate limit HTTP request based on IP address (for example). You also need to specify how Job's rate limit is applied. So, you need something like:

return Limit::perMinutes(
    decayMinutes: 10,
    maxAttempts: 5
)->by($job->contact->id); 

// or $job->invite->id
// or $job->contact->email
// or what ever makes sense.
1 like

Please or to participate in this conversation.