-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Bring Rate Limiters to Jobs #34829
[8.x] Bring Rate Limiters to Jobs #34829
Conversation
Have you seen the |
@taylorotwell, thanks for your feedback. I didn't know about the |
@taylorotwell I've added a new class for rate limiting jobs with Redis along with a test. Let me know if you'd like me to make any changes. |
Currently, it is possible to share the same limiter for 2 or more jobs (just like in requests). All jobs sharing the limiter would count towards the rate limiting in that case. In fact, we could even use the same limiter for a job and a request (again both the job and the request would count towards the limit). Just wanted to make a note of it here. |
Hi @paras-malhotra , great idea on bringing default job middleware to the core, congrats! In a app I am working on we have a queued job that syncs some information with an external API, so after migrating this project to Laravel 8 I wrote a similar job middleware to rate limit this, mainly because for this particular project we don't have Redis available. I shared my take on a gist (and on twitter) about 3 weeks ago: https://gist.github.com/rodrigopedra/ca36827783f1acdb065a98838014b8ce Overall your code seems more organized, but I would like to highlight some differences from my take as it could aggregate some ideas if you find them helpful.
I have some other minor differences specific to my use-case and client's requirements that I don't think need to make into core, for example:
Hope it helps somehow, and again congrats on the initiative on bringing this middlewares into the core. |
Hi @rodrigopedra, thanks for your feedback!
Yes, I thought about that but job middlewares are constructed in userland (in the middleware method of the job class), unlike request middleware where the container is injected in the constructor
That's a good point. I made it similar to |
Thanks for the response @paras-malhotra Although job middlewares are constructed in the job itself, they are run through the As such, if we pass a FQCN, with optional additional parameters, any constructor dependecies will be provided by the pipeline's container instance. You can see in my implementation that the middleware expect a RateLimiter instance, and that I use the static method to build its FQCN with the parameters so there is no need to use the Container singleton there. Personally I don’t see a problem on using the Container singleton there, when I implemented that I inspired myself on other existing middleware in the corresponding project. Just thought on sharing so it could provide any good idea. One posibility is sharing middlewares betwen jobs and requests. For example instead of releasing the job righr away we could throw an exception and release the job on its failed method. That way the job could be used as a request middleware witout major changes. But anyway I think this is a great addition to the framework. |
Ahh okay, Thanks so much for the insights @rodrigopedra! |
I did add some buffer seconds like @rodrigopedra's implementation. |
Also renamed classes to |
Hi, @paras-malhotra |
@aachekalov, this is not supported yet. I've just submitted a PR to allow this (#35010). |
Thank you very much! That is just what I need! |
How to test job has middleware with Bus::fake()? when use Bus::fake, the middleware looks not called. |
We already have rate limiters available for the request throttling middleware. This PR enables the same rate limiter classes and syntax to be used for rate limiting jobs!
You may define named rate limiters in your app service provider:
You can attach these named rate limiters to jobs by passing their names to the RateLimitsJobs middleware: