I created an API using Django Rest Framework, now i'm working on a rate limiting system, to avoid spam. The built-in throttling system works great, and i managed to add multiple throttles:
REST_FRAMEWORK = {
# 'DEFAULT_AUTHENTICATION_CLASSES': (
# "xapi.authentication_backends.TokenBackend",
# ),
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '70/minute',
'user': '70/minute',
'user_sec': '2/second',
'user_min': '120/minute',
'user_hour': '7200/hour',
},
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
)
}
And in my views.py:
class UserSecThrottle(UserRateThrottle):
scope = 'user_sec'
class UserMinThrottle(UserRateThrottle):
scope = 'user_min'
class UserHourThrottle(UserRateThrottle):
scope = 'user_hour'
So if some user performs more than 120 queries in a minute, that user will be blocked for a minute, if the hour limit is breached, the user is blocked for one hour. Is there some way to decide for how much is a user blocked? For example, if i want to block someone for 10 minutes if they perform more than 120 queries in a minute. Any advice is appreciated.