Search code examples
laravelqueuelimitjobs

Limit Requests to an External API in Laravel Using Job and Queue


I am using Laravel 6 and I have an external API. I want to limit the requests to this API to one request per 10 seconds.This API is called inside a job. How can I achieve this?


Solution

  • With Laravel 6 you can delay the job. So you do like:

    JobClass::dispatch($payload)->delay(now()->addSeconds(10));

    You may throttle a given type of job to only run 1 time every 10 seconds. So to impose this throttle it requires redis and you do like:

    Redis::throttle('key')->allow(1)->every(10)->then(function () {

    You can set time to the failed jobs so it takes time.

    /**
     * The number of seconds to wait before retrying the job.
     *
     * @var int
     */
    public $retryAfter = 10;