-
Notifications
You must be signed in to change notification settings - Fork 187
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
Add async support? #293
Comments
Hi @mlissner, thanks for raising this! I'm definitely open to adding async support here. I'd love to know what isn't working—I'm guessing decorating an async function, but maybe there are other things?—and how you think the API might need to change? |
I'm not an expert here, but yes, decorating an async function is where we ran into trouble. There's some prior art in https://pypi.org/project/django-fast-ratelimit/, where their decorator can work in both sync or async, but I haven't looked to see how that works. That does feel like the ideal solution though. A less elegant solution might be to have an |
Gotcha, thank you! There was a similar problem in the |
I don't think that's necessarily less elegant, it could be very useful and in any case, it's also very explicit, given that people know the convention :) Prefixing with an |
Yeah, it's not horrible, but it would have been really nice to take a view like this:
And make it async by just doing:
I think that's pretty cool, though I agree that a-prfixing it isn't the end of the world. :) |
@mlissner I believe we can support that API using the same technique as the pystatsd timer class. But, I think I might need more help understanding exactly how the failure is happening right now. In #295 I tried adding a test that I thought would fail, but it doesn't—and when I think about it, I understand why it doesn't. Can you share a little more about the issues you're seeing and the expected/actual behavior? |
Hi Guys, I hope you are ok. |
Hi @mchingotto would you mind opening another issue for that? It's a pretty different situation |
I modified that test in #298 and got something that fails when expected now by asserting that the function is actually async(which is how django determines how it should be called). |
I've got some code that's been working for us I can put in a pr. It follows how django makes async decorators, so it should generally work alright, but please feel free to double check it. |
#300 Should fix this as long as I can get confirmation from someone else that this works for them too. I'll be testing it on my work project to see if it helped. |
Thanks, folks! Just to make sure I understand what the issues are, let me paraphrase:
That sound right? The API in #300 ( |
Actually no, the async view does not work if we wrap it in a sync decorator because it doesn't await the view. This is why I did this in the first place as we didn't have a way to use this. As for the second point, yes. Although it looks like aincr doesn't exist from what I saw? if it does then i'm happy to change that line. That's fair that you want to eliminate duplication. I tried my hardest to, hence why there is a function that accepts an is_async argument so we don't have so much code reuse. I'm not the biggest fan of the fact that in python we have to have a sync and async version of each function, but it is what it is. Let me know if there is anything else I can help with. I'm excited to have this merged so our project can use it and we don't have to keep the code locally lol. |
I added some comments on the PR. I hope they're helpful and move this forward a bit. Also to give examples of how async views decorated with this package fail, I can share this issue: freelawproject/courtlistener#2930 |
Hi all, I just wanted to follow up. We've been using the usual ratelimit decorator on our views at work, and it'd really be nice to get an async version put in place. If you use the regular decorator, like we do, it sort of works — whenever the rate limiter is hit, the view crashes. Could be worse, but that sends a Sentry event for us, which isn't great either. @daniel-brenot are you still using your version from #300 and is it working properly? If #300 is working for @daniel-brenot for the past month+, maybe we should merge it and give it some more light of day? |
My fork has been updated with the async versions of all the existing tests. If this is satisfactory, i'd recommend a look over |
I just added a comment to your PR, @daniel-brenot, but I hope we can get his out the door soon. Anything my organization can do to help? |
Nope. The PR is fully tested and just waiting on approval. I don't anticipate any issues with the PR, it passes all of the tests i rewrote for async so it seems to work fine. |
We are using this package with great success on our website, but as we move to the async model, we're finding that it doesn't work.
If we added async support to this package, would that be welcomed by the maintainers? If so, we can probably take a stab at doing so. If not — and that's perfectly OK! — we may consider making a small fork with that functionality and calling it django-aratelimit, or something like that. (Haven't given this too much thought, since it'd be much better to add the functionality here!)
Any thoughts or suggestions about doing this before we go further down this road?
Thank you for the great package.
The text was updated successfully, but these errors were encountered: