comparison roundup/rate_limit.py @ 5739:e225f403cc35

Run pylint and clean up it's issues. Also fix comment.
author John Rouillard <rouilj@ieee.org>
date Tue, 28 May 2019 18:10:30 -0400
parents 2f116ba7e7cf
children 5d0873a4de4a
comparison
equal deleted inserted replaced
5738:9777d7311bc0 5739:e225f403cc35
4 # set/get_tat and marshaling as string, support for testonly 4 # set/get_tat and marshaling as string, support for testonly
5 # and status method. 5 # and status method.
6 6
7 from datetime import timedelta, datetime 7 from datetime import timedelta, datetime
8 8
9 class RateLimit: 9 class RateLimit: # pylint: disable=too-few-public-methods
10 def __init__(self, count, period): 10 def __init__(self, count, period):
11 self.count = count 11 self.count = count
12 self.period = period 12 self.period = period
13 13
14 @property 14 @property
15 def inverse(self): 15 def inverse(self):
16 return self.period.total_seconds() / self.count 16 return self.period.total_seconds() / self.count
17 17
18 18
19 class Gcra: 19 class Gcra:
20 20 def __init__(self):
21 memory = {} 21 self.memory = {}
22 22
23 def get_tat(self, key): 23 def get_tat(self, key):
24 # This should return a previous tat for the key or the current time. 24 # This should return a previous tat for the key or the current time.
25 if key in self.memory: 25 if key in self.memory:
26 return self.memory[key] 26 return self.memory[key]
45 # YYYY-MM-DDTHH:MM:SS.mmmmmm 45 # YYYY-MM-DDTHH:MM:SS.mmmmmm
46 # to datetime 46 # to datetime
47 self.memory[key] = datetime.strptime(tat,"%Y-%m-%dT%H:%M:%S.%f") 47 self.memory[key] = datetime.strptime(tat,"%Y-%m-%dT%H:%M:%S.%f")
48 48
49 def update(self, key, limit, testonly=False): 49 def update(self, key, limit, testonly=False):
50 '''Determine if the item assocaited with the key should be 50 '''Determine if the item associated with the key should be
51 rejected given the RateLimit limit. 51 rejected given the RateLimit limit.
52 ''' 52 '''
53 now = datetime.utcnow() 53 now = datetime.utcnow()
54 tat = max(self.get_tat(key), now) 54 tat = max(self.get_tat(key), now)
55 separation = (tat - now).total_seconds() 55 separation = (tat - now).total_seconds()
89 89
90 ret['X-RateLimit-Remaining'] = min(int( 90 ret['X-RateLimit-Remaining'] = min(int(
91 (limit.period - (tat - now)).total_seconds() \ 91 (limit.period - (tat - now)).total_seconds() \
92 /limit.inverse),ret['X-RateLimit-Limit']) 92 /limit.inverse),ret['X-RateLimit-Limit'])
93 93
94 tat_epochsec = (tat - datetime(1970, 1, 1)).total_seconds() 94 # tat_in_epochsec = (tat - datetime(1970, 1, 1)).total_seconds()
95 seconds_to_tat = (tat - now).total_seconds() 95 seconds_to_tat = (tat - now).total_seconds()
96 ret['X-RateLimit-Reset'] = max(seconds_to_tat, 0) 96 ret['X-RateLimit-Reset'] = max(seconds_to_tat, 0)
97 ret['X-RateLimit-Reset-date'] = "%s"%tat 97 ret['X-RateLimit-Reset-date'] = "%s"%tat
98 ret['Now'] = (now - datetime(1970,1,1)).total_seconds() 98 ret['Now'] = (now - datetime(1970,1,1)).total_seconds()
99 ret['Now-date'] = "%s"%now 99 ret['Now-date'] = "%s"%now

Roundup Issue Tracker: http://roundup-tracker.org/