summaryrefslogtreecommitdiffstats
path: root/pyload/network/Bucket.py
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@gmail.com> 2014-12-11 17:03:31 +0100
committerGravatar Walter Purcaro <vuolter@gmail.com> 2014-12-11 17:03:31 +0100
commit5ae784da28024596e5f2621179625728cdc1e8f6 (patch)
tree273ae5dd85c8633d55491e438d893ee3434a8024 /pyload/network/Bucket.py
parentClean up some import header (diff)
downloadpyload-5ae784da28024596e5f2621179625728cdc1e8f6.tar.xz
Bucket and CookieJar from 0.5.0
Diffstat (limited to 'pyload/network/Bucket.py')
-rw-r--r--pyload/network/Bucket.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/pyload/network/Bucket.py b/pyload/network/Bucket.py
index 047329ad8..3aef2c733 100644
--- a/pyload/network/Bucket.py
+++ b/pyload/network/Bucket.py
@@ -2,28 +2,37 @@
# @author: RaNaN
from time import time
-from threading import Lock
-class Bucket(object):
+
+# 10kb minimum rate
+MIN_RATE = 10240
+
+
+class Bucket:
+
def __init__(self):
- self.rate = 0
- self.tokens = 0
+ self.rate = 0 # bytes per second, maximum targeted throughput
+ self.tokens = 0
self.timestamp = time()
- self.lock = Lock()
+ self.lock = Lock()
+
def __nonzero__(self):
- return False if self.rate < 10240 else True
+ return False if self.rate < MIN_RATE else True
+
def setRate(self, rate):
self.lock.acquire()
self.rate = int(rate)
self.lock.release()
+
def consumed(self, amount):
- """ return time the process have to sleep, after consumed specified amount """
- if self.rate < 10240: return 0 #min. 10kb, may become unresponsive otherwise
- self.lock.acquire()
+ """ return the time the process has to sleep, after it consumed a specified amount """
+ if self.rate < MIN_RATE:
+ return 0 #: May become unresponsive otherwise
+ self.lock.acquire()
self.calc_tokens()
self.tokens -= amount
@@ -32,10 +41,10 @@ class Bucket(object):
else:
time = 0
-
self.lock.release()
return time
+
def calc_tokens(self):
if self.tokens < self.rate:
now = time()