-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtoken_bucket.py
More file actions
50 lines (39 loc) · 1.29 KB
/
Copy pathtoken_bucket.py
File metadata and controls
50 lines (39 loc) · 1.29 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from time import time
class TokenBucket(object):
"""
An implementation of the token bucket algorithm.
"""
def __init__(self, n_req):
"""
Initialize a request limiting token bucket.
Input:
n_req number of requests per second (integer)
"""
self.capacity = n_req
self._n_req = n_req
self.fill_rate = float(n_req)
self.timestamp = time()
def may_i(self):
"""
Consume tokens from the bucket.
Returns True if there were sufficient tokens otherwise False.
"""
if self.n_req > 0:
self._n_req -= 1
else:
return False
return True
def get_tokens(self):
"""
Update and return the current number of available tokens/requests
"""
if self._n_req < self.capacity:
now = time()
# how many tokens have been made available since the last comsume
delta = self.fill_rate * (now - self.timestamp)
# update the number of availabe tokens (limited by max capacity)
self._n_req = min(self.capacity, self._n_req + delta)
# update timestamp of last consume
self.timestamp = now
return self._n_req
n_req = property(get_tokens)