diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-12-19 19:59:45 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-12-19 19:59:45 +0100 |
commit | 34a5b4372aa6e50c072a568c564d6d492743c508 (patch) | |
tree | b7ace2ebd3511b4165cccfb6b44d96c60963dc14 /module/network/Bucket.py | |
parent | corrected byte range on resume (diff) | |
download | pyload-34a5b4372aa6e50c072a568c564d6d492743c508.tar.xz |
some adjusments
Diffstat (limited to 'module/network/Bucket.py')
-rw-r--r-- | module/network/Bucket.py | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/module/network/Bucket.py b/module/network/Bucket.py index 35e27bcd4..c7eb62a54 100644 --- a/module/network/Bucket.py +++ b/module/network/Bucket.py @@ -22,7 +22,7 @@ from threading import Lock class Bucket: def __init__(self): - self.content = 0 + self.tokens = 0 self.rate = 0 self.lastDrip = time() self.lock = Lock() @@ -32,22 +32,22 @@ class Bucket: self.rate = rate self.lock.release() - def add(self, amount): + def consume(self, amount): + """ consume specified amount, return False if not enough tokens in bucket """ + if not self.rate: return True self.lock.acquire() - self.drip() - allowable = min(amount, self.rate - self.content) - if allowable > 0: - sleep(0.005) #@XXX: high sysload without?! + if amount < self.getTokens(): + self.tokens -= amount + self.lock.release() + return True - self.content += allowable self.lock.release() - return allowable + return False - def drip(self): - if self.rate == 0: - self.content = 0 - else: + def getTokens(self): + if self.tokens < self.rate: now = time() - deltaT = now - self.lastDrip - self.content = long(max(0, self.content - deltaT * self.rate)) + delta = self.rate * (now - self.lastDrip) + self.tokens = min(self.rate, self.tokens + delta) self.lastDrip = now + return self.tokens |