4

Trying to implement Rate Limiting for the queue to run one job per second that makes an HTTP request to external API and load one types of data.

But not getting how to call the job, tried different option but not working. In the example:

Redis::throttle('key')->allow(10)->every(60)->then(function () {
    // Job logic...
}, function () {
    // Could not obtain lock...
    return $this->release(10);
});

What will put in // Job logic... dispatch the queue and this code will be within the queue class? and how to name the key? my queue name is loader.

Any help?

Meathanjay
  • 2,023
  • 1
  • 18
  • 24

2 Answers2

7

Put it inside the handle method of the Job.

public function handle() {
    Redis::throttle('key')->allow(10)->every(60)->then(function () {
        // Job logic...
    }, function () {
        // Could not obtain lock...
        return $this->release(10);
    });
}

The key should be any unique string. It will identify the restriction: "allow(10)->every(60)".

Laraleg
  • 519
  • 3
  • 7
  • https://gist.github.com/meathanjay/3ab3a40efe5a67bce840b69f310590d8 here is sample implementation of `handle` method and the key is unique but it doesn't get through limit, 10 every 60 seconds, instead execute instantly – Meathanjay Nov 20 '17 at 21:35
  • for some of us on Laravel 5.3, what alternative do we have? – Jimmy Ilenloa Jan 14 '18 at 15:31
  • 1
    @Meathanjay I think the key is your problem: it should be unique per type of job, not for each job instance. So with unique keys for every single job, throttling will never occur as the limit will never be hit.. – 4levels Feb 13 '19 at 12:54
  • @Laraleg can you explain what this part does? `$this->release(10);` and, is the arg in the `release` method always the same as in the `allow` method? – andrux Jul 16 '19 at 00:17
  • 2
    @andrux it puts the job back to the queue to be processed in 10 seconds – Laraleg Jul 16 '19 at 17:32
0

You can also use this package to use rate limiting with Redis or another source. Uses settings to set bucket size and rate as fractions of the time limit, so very small storage.

composer require bandwidth-throttle/token-bucket

https://github.com/bandwidth-throttle/token-bucket

tristanbailey
  • 4,427
  • 1
  • 26
  • 30