The cache backend must be shared (Redis), otherwise each worker counts separately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # ====================================================================== # Q: How do you throttle per API key for B2B customers? # # The cache backend must be shared (Redis), otherwise each worker counts # separately. It returns 429 with Retry-After. # ====================================================================== class B2BKeyThrottle(SimpleRateThrottle): scope = 'b2b' def get_cache_key(self, request, view): key = getattr(request.auth, 'key_id', None) return self.cache_format % {'scope': self.scope, 'ident': key or self.get_ident(request)} REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': ['api.throttles.B2BKeyThrottle'], 'DEFAULT_THROTTLE_RATES': {'b2b': '600/min'}, } |