From fe183fd3d21bd89d2dc65fb80aada4685110c70d Mon Sep 17 00:00:00 2001 From: JasperSui Date: Fri, 9 Jul 2021 10:57:33 +0800 Subject: [PATCH] Replace SimpleThrottle self.history default value with deque --- rest_framework/throttling.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 0ba2ba66b..dd4f062b4 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -2,6 +2,7 @@ Provides various throttling policies. """ import time +from collections import deque from django.core.cache import cache as default_cache from django.core.exceptions import ImproperlyConfigured @@ -120,7 +121,7 @@ class SimpleRateThrottle(BaseThrottle): if self.key is None: return True - self.history = self.cache.get(self.key, []) + self.history = self.cache.get(self.key, deque()) self.now = self.timer() # Drop any requests from the history which have now passed the @@ -136,7 +137,7 @@ class SimpleRateThrottle(BaseThrottle): Inserts the current request's timestamp along with the key into the cache. """ - self.history.insert(0, self.now) + self.history.appendleft(self.now) self.cache.set(self.key, self.history, self.duration) return True