@@ -11,30 +11,51 @@ class TokenBucket(object):
1111 False
1212 """
1313
14- def __init__ (self , tokens , fill_rate ):
14+ def __init__ (self , _capacity , fill_rate ):
15+ """
16+ :param _capacity: The total amount of token the bucket can contain
17+ :param fill_rate: The rate at which tokens regenerate. (fill_rate per second)
18+ """
1519 """tokens is the total tokens in the bucket. fill_rate is the
1620 rate in tokens/second that the bucket will be refilled."""
17- self .capacity = float (tokens )
18- self ._tokens = float (tokens )
21+ self .capacity = float (_capacity )
22+ self ._tokens = float (_capacity )
1923 self .fill_rate = float (fill_rate )
2024 self .timestamp = time ()
2125
2226 def consume (self , tokens ):
23- """Consume tokens from the bucket. Returns True if there were
24- sufficient tokens otherwise False."""
27+ """
28+ Consume tokens from the bucket.
29+ :param tokens: The number of tokens to consume
30+ :return true if there were sufficient tokens otherwise false
31+ """
2532 if tokens <= self .tokens :
2633 self ._tokens -= tokens
2734 else :
2835 return False
2936 return True
3037
3138 def refill (self ):
39+ """
40+ Sets the current token count to the max capacity
41+ """
3242 self ._tokens = self .capacity
43+ return True
3344
3445 def empty (self ):
46+ """
47+ Sets the current token count to zero
48+ """
3549 self ._tokens = float (0 )
50+ return True
3651
3752 def get_tokens (self ):
53+ """
54+ Calculates and returns the current amount of tokens the bucker contains
55+
56+ :return Amount of tokens the bucket contains
57+ :rtype Float
58+ """
3859 now = time ()
3960 if self ._tokens < self .capacity :
4061 delta = self .fill_rate * (now - self .timestamp )
0 commit comments