Laravel queue rate limiting or throttling

后端 未结 6 1321
南方客
南方客 2020-12-11 17:19

I am working on an app that requires fetching data from a third-party server and that server allows max 1 request per seconds.

Now, all request send as job and I am

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 17:32

    I am rate limiting a job que to max out at 100 jobs per day. I chose to use the 'delay' feature.

    1. I have an ORM class for the jobs table.
    2. I lookup the last job on the throttled queue.
    3. Increment $last_job->available_at by 15 minutes
    4. pass the result into the new job as a delay before it goes on the queue.

    Job Class

    Sample delay without much context...

    $que = config(ServiceProvider::SHORT_NAME . '.job-queue');
    
    
    $preceeding_job = Job::whereQueue($que['name'])
                         ->orderBy('available_at', 'DESC')
                         ->first();
    
    if(!empty($preceeding_job)){
        $available = $preceeding_job->available_at;
    }else{
        $available = Carbon::now();
    }
    
    
    ProbeGoogle::dispatch($obj)
               ->onConnection($que['connection'])
               ->onQueue($que['name'])
               ->delay($available->addMinutes(15));
    

    Note that completed jobs are removed from the jobs table, so you need another means of tracking them and a bit more logic to calculate $available. I use https://github.com/imTigger/laravel-job-status for that. For this example, I went with simple logic. It will either fire 15 min after the last job in the queue, or 15 min from now. That helps pace things out just in case the last job fired and disappeared 2 seconds ago.

提交回复
热议问题