laravel / horizon

Dashboard and code-driven configuration for Laravel queues.
https://laravel.com/docs/horizon
MIT License
3.84k stars 645 forks source link

external api rate limiting #1483

Closed kolirt closed 1 month ago

kolirt commented 1 month ago

Hey. I have a queue that exclusively sends requests to an external server. The external server has a request limit of 30 requests per second. Is it possible to configure the worker configuration so that it does not exceed this limit?

driesvints commented 1 month ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

kolirt commented 1 month ago

@driesvints

For several days I have not found any solution to my problem with the horizon. Could you please include this issue in a feature request?

driesvints commented 1 month ago

@kolirt if you want to add something to Horizon please attempt a PR, thanks.

juse-less commented 1 month ago

@kolirt it doesn't solve the whole issue, but..

The way I solved it was to use the regular rate limit middleware in Laravel, and added to the jobs. https://laravel.com/docs/11.x/queues#rate-limiting

So something like this

// In a service provider.
public function boot(): void
{
    // Adjust if you make more than one request per job.
    RateLimiter::for('service-name', fn (): Limit => Limit::perSecond(30));
}

// Job class
public function middleware(): array
{
    return [new RateLimited('service-name')];
}

It's far from perfect but has worked pretty good for me, so I haven't had to look into a proper solution. I have different limiters for different services, but I also only use one service in each job.