Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davidifranco's avatar

Dynamically Throttle Jobs using Redis

Hi I'm working on throttling the amount of times a user can send out email in a 24 hr period (based on his service provider... ie gmail, outlook, aol.. etc.). Emails are sent as queued jobs and ive set up my throttle as follows:

$limit = ($this->account->Provider->max_daily_emails/24/60);

foreach ($this->recipients as $recipient) {
    Redis::throttle($this->account->username . '_email_limit')
            ->allow($limit)
            ->every(60)
            ->then(function () use ($recipient) {

                            config()->set('mail', array_merge(config('mail'), [
                                <<-- users email config -->>
                            ]));

                             $unsubscribe_link = URL::signedRoute('unsubscribe', ['recipient' => $recipient, 'campaign' => $this->campaign]);

                             Mail::to($recipient->email)->send(new CampaignEmail($this->campaign, $unsubscribe_link, $this->account->full_name, $recipient));

                            unset($unsubscribe_link);  

                             $this->campaign->Recipients()->updateExistingPivot($recipient->id, ['sent' => now()]);

                    }, function () {

                        return $this->release(60);

                    });
}

Assuming the provider allows 500 email to be sent per day... my throttle->allow() limit equates to .3472 emails per 60 seconds... will redis then allow 1 email every 3 minutes. I guess i still haven't figured out exactly how ''' allow(); every(); block(); and release(); ''' work. Would appreciate any insight... Thanks in advance.

0 likes
0 replies

Please or to participate in this conversation.