django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Global per-resource throttling

Open markotibold opened this issue 15 years ago • 6 comments

It would be nice to be able to throttle requests to a resource, not only per user. Only question is: should we store the throttle in the cache based on the url of the resource or based on the View's class....

markotibold avatar Jun 11 '11 00:06 markotibold

implemented in the permission module. Plus wrote a test. Both test and implementation need refactoring.

markotibold avatar Jun 11 '11 01:06 markotibold

I would probably store it based on the name of the Resource class. Actually even better would be if the throttle has a get_cache_key() method. For per-user it'd return the user id, for the per-resource it'd return the Resource class name - then have both Thottle classes inherit from a BaseThrottle class. Then it's super obvious and easy for anyone to provide for alternative caching behavior. Actually thinking about it there's a few ways that a BaseThrottle class could be made easier to override.

I think it's refactor nicely to:

BaseThrottle(BasePermission): get_cache_key(self) # Not Implemented check_throttle(self, cache_key) throttle_success(self, cache_key) throttle_failure(self, cache_key)

PerUserThrottle(BaseThrottle): get_cache_key(self)

PerResourceThrottle(BaseThrottle): get_cache_key(self)

Also thinking about it I think it'd be nice if the .throttle attribute was string like '10/sec', '45/min', '100/hour', '100/day', '10/s' '45/m' etc... instead of being a 2-tuple

So something like:

num, period = getattr(self.view, 'throttle', '0/sec').partition('/') num = int(num) period = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]

On 11 June 2011 01:22, markotibold < [email protected]>wrote:

It would be nice to be able to throttle requests to a resource, not only per user. Only question is: should we store the throttle in the cache based on the url of the resource or based on the View's class....

Reply to this email directly or view it on GitHub: https://github.com/tomchristie/django-rest-framework/issues/25

lovelydinosaur avatar Jun 11 '11 11:06 lovelydinosaur

Yeah that's a good idea!

But to return self.view.resource.__class__.__name__ in the gey_cache_key() method would imply the View actually has a resource set. But this is not necessarily the case. We can have it store the name of the view then instead. Any thoughts?

markotibold avatar Jun 11 '11 13:06 markotibold

Fair point. Would be fine yeah. Call it PerViewThrottling tho' ;)

lovelydinosaur avatar Jun 11 '11 17:06 lovelydinosaur

Ok, I got it all implemented and tested. Check out my latest commits. Did some proper refactoring on last nights work.

Cheers,

Mako

On 11 June 2011 19:44, tomchristie < [email protected]>wrote:

Fair point. Would be fine yeah. Call it PerViewThrottling tho' ;)

Reply to this email directly or view it on GitHub:

https://github.com/tomchristie/django-rest-framework/issues/25#issuecomment-1352274

markotibold avatar Jun 11 '11 18:06 markotibold

Oops, ignore my latest github ticket then :) Nice work. I'll pull this bit in tomorrow...

On 11 June 2011 19:36, markotibold < [email protected]>wrote:

Ok, I got it all implemented and tested. Check out my latest commits. Did some proper refactoring on last nights work.

Cheers,

Mako

On 11 June 2011 19:44, tomchristie < [email protected]>wrote:

Fair point. Would be fine yeah. Call it PerViewThrottling tho' ;)

Reply to this email directly or view it on GitHub:

https://github.com/tomchristie/django-rest-framework/issues/25#issuecomment-1352274

Reply to this email directly or view it on GitHub:

https://github.com/tomchristie/django-rest-framework/issues/25#issuecomment-1352429

lovelydinosaur avatar Jun 11 '11 19:06 lovelydinosaur