Created
October 1, 2012 10:02
-
-
Save giannitedesco/3810656 to your computer and use it in GitHub Desktop.
python: Token bucket filter with burst and sleep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
class TokenBucket: | |
from time import sleep, time | |
def __init__(self, rate, burst = None): | |
'Rate in tokens per second, burst in number of tokens' | |
self.rate = float(rate) | |
if burst is None: | |
burst = self.rate | |
self.rate = 1 / self.rate | |
self.burst = float(burst) * self.rate | |
self.toks = self.burst | |
self.last = self.now() | |
def now(self): | |
return self.time() | |
def rate_limit(self, tokens = 1.0): | |
tokens = float(tokens) | |
if self.rate <= 0: | |
return | |
h = self.now() | |
diff = h - self.last | |
self.toks += diff | |
self.last = h | |
if self.toks > self.burst: | |
self.toks = self.burst | |
if self.toks >= self.rate: | |
self.toks -= self.rate | |
if self.toks < self.rate: | |
print 'sleep for %f'%(self.rate - self.toks) | |
self.sleep((self.rate - self.toks)) | |
if __name__ == '__main__': | |
tbf = TokenBucket(2.0, 5.0) | |
while True: | |
tbf.rate_limit() | |
print '.' | |
raise SystemExit, 0 |
Haha
Cool!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
potential division by zero on line 12!