boyenec
boyenec

Reputation: 1617

Django DRF how to overwrite Custom throttles

I want to implement this Custom throttles. I want to disallow every request if it exceeded a certain number of seconds. For example, If anyone sends more than 10 requests per seconds then I want to stop and wait 20seconds before sending new request. I tried this code but didn't work

class CstomRateThrottle(throttling.BaseThrottle):
    def allow_request(self, request, view, wait=20):
        return True

They saying Optionally I may also override the .wait() method but don't know how.

I also tried this but didn't work

class CstomRateThrottle(throttling.AnonRateThrottle):
    def parse_rate(self, rate=(10, 20)): #10 request in very 20 seconds 

        if rate is None:
            return (None, None)
        num, period = rate.split('/')
        num_requests = int(num)
        duration = timeparse(period)
        return (num_requests, duration)

Upvotes: 0

Views: 482

Answers (1)

Metalgear
Metalgear

Reputation: 3457

I think you need to customize the wait function like the following.

class CstomRateThrottle(throttling.BaseThrottle):
    def wait(self):
        return 10

Upvotes: 1

Related Questions