V
V
Vasyl Fomin2020-10-28 11:32:00
Laravel
Vasyl Fomin, 2020-10-28 11:32:00

How to implement sending data with a limit on the number of requests to an external service - 10 requests per second?

Hello! You need to integrate a mailing service (in this case, SendPulse) into the system (Laravel), into which customer contacts will be uploaded and updated.
In doc. SendPulse states that they have a "Request limit - 10 requests per second.".
How is it possible to implement in my Laravel system so that it does not make more than 10 requests per second. to the external SendPulse service. I understand that it is possible to somehow throw tasks into the queue, but I can’t understand how to perform it with such a restriction?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pLavrenov, 2020-10-29
@pLavrenov

To do this, you need to use middleware for job. I don't know what version it came in.
The middleware() method is added to the required Job

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

the middleware itself looks like this...
class LimitJobThrottleMiddleware
{
    public function handle($job, $next)
    {
        Redis::throttle('throttle:sync')
            ->allow(10) // Разрешено максимум 10 тасков
            ->every(1) // каждую секунду
            ->then(fn() => $next($job), fn() => $job->release());
    }
}

Important! throttle:sync - must be unique. Everything that exceeds the limit is sent back to the queue and waits when it will be possible.

P
Peter, 2020-10-28
@petermzg

You have to cook jam for an hour, you need to stir it so that it doesn’t burn, but you can’t stir it more than 10 times in this hour, how to do it?
Available:
- Jam,
- Spoon
- Clock

A
Anton Shamanov, 2020-10-28
@SilenceOfWinter

store the time of the last request, although it's easier to choose one of the options:
1. in case of an error from SendPulse with the "number of requests exceeded" code, display a message stating that sending is possible only after a couple of seconds.
2. if sending through the web interface, then send ajax requests until SendPulse returns the 2xx code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question