forked from IvorySQL/ivorysql-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter.py
More file actions
25 lines (19 loc) · 656 Bytes
/
Copy pathlimiter.py
File metadata and controls
25 lines (19 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import time
class RateLimitingState(object):
def __init__(self, rate, clientip, name):
self.name = name
self.clientip = clientip
self.rate = rate
self.allowance = rate
self.last_check = time.time()
def do_throttle(self, message):
current = time.time()
time_passed = current - self.last_check
self.last_check = current
self.allowance += time_passed * self.rate
if self.allowance > self.rate:
self.allowance = self.rate #throttle
if self.allowance > 1.0:
self.allowance -= len(message)
return True;
return False