开发者

How to limit requests per minute per user?

开发者 https://www.devze.com 2023-01-02 05:20 出处:网络
I Have several forms in my website and I have several pages with intense database activity. I want to set a cap on requests per user. For example, I don\'t want people to make over 10 requests in less

I Have several forms in my website and I have several pages with intense database activity. I want to set a cap on requests per user. For example, I don't want people to make over 10 requests in less than 10 seconds.

I开发者_开发百科s there way to do this in Django?


You can likely do this with custom middleware. You'll need to keep the data somewhere (db?). See the docs for how to write your own middleware. Here's what's available to you in the request object.

I'd recommend doing this on apache/nginx/whatever you're using, though.


Have a look at Simon Willison's ratelimitcache. Like the name implies, it uses Django's cache framework to store the state of the rate limiter. There's also a blog post that details how the rate limiter works.

0

精彩评论

暂无评论...
验证码 换一张
取 消