summaryrefslogtreecommitdiffstats
path: root/module/network/Bucket.py
diff options
context:
space:
mode:
authorGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2010-12-19 21:09:08 +0100
committerGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2010-12-19 21:09:08 +0100
commit75777c1089ce1fd2d25865442707f1facf53e8c5 (patch)
treec7e1403c76e916d8eef6e3b5bf66c5e20746ee1c /module/network/Bucket.py
parentnew network backend: xdcc support (diff)
downloadpyload-75777c1089ce1fd2d25865442707f1facf53e8c5.tar.xz
bucket improvement
Diffstat (limited to 'module/network/Bucket.py')
-rw-r--r--module/network/Bucket.py30
1 files changed, 14 insertions, 16 deletions
diff --git a/module/network/Bucket.py b/module/network/Bucket.py
index c7eb62a54..04b706ff2 100644
--- a/module/network/Bucket.py
+++ b/module/network/Bucket.py
@@ -22,32 +22,30 @@ from threading import Lock
class Bucket:
def __init__(self):
- self.tokens = 0
+ self.content = 0
self.rate = 0
self.lastDrip = time()
self.lock = Lock()
def setRate(self, rate):
- self.lock.acquire()
self.rate = rate
- self.lock.release()
- def consume(self, amount):
- """ consume specified amount, return False if not enough tokens in bucket """
- if not self.rate: return True
+ def add(self, amount):
self.lock.acquire()
- if amount < self.getTokens():
- self.tokens -= amount
- self.lock.release()
- return True
+ self.drip()
+ allowable = min(amount, self.rate - self.content)
+ if allowable < 3072:
+ allowable = 0
+ self.content += allowable
self.lock.release()
- return False
+ return allowable
- def getTokens(self):
- if self.tokens < self.rate:
+ def drip(self):
+ if self.rate:
now = time()
- delta = self.rate * (now - self.lastDrip)
- self.tokens = min(self.rate, self.tokens + delta)
+ deltaT = now - self.lastDrip
+ self.content = long(max(0, self.content - deltaT * self.rate))
self.lastDrip = now
- return self.tokens
+ else:
+ self.content = 0