Files
GNX-WEB/gnx-react/backend/gnx/throttling.py
Iliyan Angelov 76c857b4f5 update
2025-10-10 21:54:39 +03:00

62 lines
1.3 KiB
Python

"""
Rate Limiting for Enterprise Security
Prevents abuse and DDoS attacks
"""
from rest_framework.throttling import SimpleRateThrottle
class BurstRateThrottle(SimpleRateThrottle):
"""
Short-term burst protection
"""
scope = 'burst'
def get_cache_key(self, request, view):
if request.user.is_authenticated:
ident = request.user.pk
else:
ident = self.get_ident(request)
return self.cache_format % {
'scope': self.scope,
'ident': ident
}
class SustainedRateThrottle(SimpleRateThrottle):
"""
Long-term sustained rate limiting
"""
scope = 'sustained'
def get_cache_key(self, request, view):
if request.user.is_authenticated:
ident = request.user.pk
else:
ident = self.get_ident(request)
return self.cache_format % {
'scope': self.scope,
'ident': ident
}
# Add to settings_production.py:
"""
REST_FRAMEWORK = {
...
'DEFAULT_THROTTLE_CLASSES': [
'gnx.throttling.BurstRateThrottle',
'gnx.throttling.SustainedRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'burst': '60/min',
'sustained': '1000/day',
'anon': '100/hour',
'user': '1000/hour',
}
}
"""