Search code examples
pythondjangodjango-rest-frameworkthrottlingrate

Is it possible to get number of how many requests were received via throttling classes in django rest framework?


As title says, is it possible to get number of how many requests were received via throttling classes in django rest framework? For more information, for throttle classes I am using UserRateThrottle, for authentication classes I'm using TokenAuthentication, for permission classes I am using IsAuthenticated. I only have been able to get the limit number for example 1000/day. I did try to find the solution on google but I am out of luck! Thanks!


Solution

  • Not out of the box, but you can easily implement a solution yourself.

    UserRateThrottle is a subclass of SimpleRateThrottle, which contains methods throttle_success and throttle_failure

    You can re-implement both and add in your own logic for logging, storing a counter in cache, or something else via

    def throttle_success(self):
        log.info("throttle success")
        # alternatively, increment a counter somewhere
    
        # call super implementation
        super().throttle_success()