Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Provides various throttling policies. """
""" Rate throttling of requests. """ """ Return `True` if the request should be allowed, `False` otherwise. """ raise NotImplementedError('.allow_request() must be overridden')
""" Optionally, return a recommended number of seconds to wait before the next request. """ return None
""" A simple cache implementation, that only requires `.get_cache_key()` to be overridden.
The rate (requests / seconds) is set by a `throttle` attribute on the View class. The attribute is a string of the form 'number_of_requests/period'.
Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')
Previous request information used for throttling is stored in the cache. """
self.rate = self.get_rate()
""" Should return a unique cache-key which can be used for throttling. Must be overridden.
May return `None` if the request should not be throttled. """ raise NotImplementedError('.get_cache_key() must be overridden')
""" Determine the string representation of the allowed request rate. """ msg = ("You must set either `.scope` or `.rate` for '%s' throttle" % self.__class__.__name__) raise ImproperlyConfigured(msg)
except KeyError: msg = "No default throttle rate set for '%s' scope" % self.scope raise ImproperlyConfigured(msg)
""" Given the request rate string, return a two tuple of: <allowed number of requests>, <period of time in seconds> """ return (None, None)
""" Implement the check to see if the request should be throttled.
On success calls `throttle_success`. On failure calls `throttle_failure`. """ return True
# Drop any requests from the history which have now passed the # throttle duration
""" Inserts the current request's timestamp along with the key into the cache. """
""" Called when a request to the API has failed due to throttling. """
""" Returns the recommended next request time in seconds. """ else: remaining_duration = self.duration
""" Limits the rate of API calls that may be made by a anonymous users.
The IP address of the request will be used as the unique cache key. """
if request.user.is_authenticated(): return None # Only throttle unauthenticated requests.
ident = request.META.get('REMOTE_ADDR', None)
return self.cache_format % { 'scope': self.scope, 'ident': ident }
""" Limits the rate of API calls that may be made by a given user.
The user id will be used as a unique cache key if the user is authenticated. For anonymous requests, the IP address of the request will be used. """
else:
'scope': self.scope, 'ident': ident }
""" Limits the rate of API calls by different amounts for various parts of the API. Any view that has the `throttle_scope` property set will be throttled. The unique cache key will be generated by concatenating the user id of the request, and the scope of the view being accessed. """
# Override the usual SimpleRateThrottle, because we can't determine # the rate until called by the view.
# We can only determine the scope once we're called by the view.
# If a view does not have a `throttle_scope` always allow the request
# Determine the allowed request rate as we normally would during # the `__init__` call.
# We can now proceed as normal.
""" If `view.throttle_scope` is not set, don't apply this throttle.
Otherwise generate the unique cache key by concatenating the user id with the '.throttle_scope` property of the view. """ ident = request.user.id else:
'scope': self.scope, 'ident': ident } |