diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-12-27 21:18:29 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-12-27 21:18:29 +0100 |
commit | 9509a6444bbb538e136ed899d94aab32be629383 (patch) | |
tree | ac8532b20912a3e5be6ff73443520a7f31f5806a /module/network/Bucket.py | |
parent | encoding fix (diff) | |
download | pyload-9509a6444bbb538e136ed899d94aab32be629383.tar.xz |
new curl download backend - support for chunked dl, resume
Diffstat (limited to 'module/network/Bucket.py')
-rw-r--r-- | module/network/Bucket.py | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/module/network/Bucket.py b/module/network/Bucket.py index 434cbe662..dc1280ede 100644 --- a/module/network/Bucket.py +++ b/module/network/Bucket.py @@ -14,40 +14,44 @@ You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. - @author: mkaay + @author: RaNaN """ -from time import time, sleep +from time import time from threading import Lock class Bucket: def __init__(self): - self.content = 0 self.rate = 0 - self.lastDrip = time() + self.tokens = 0 + self.timestamp = time() self.lock = Lock() def setRate(self, rate): self.lock.acquire() self.rate = rate self.lock.release() - - def add(self, amount): + + def consumed(self, amount): + """ return time the process have to sleep, after consumed specified amount """ self.lock.acquire() - self.drip() - allowable = min(amount, self.rate - self.content) - if allowable > 0: - sleep(0.005) - self.content += allowable + self.calc_tokens() + self.tokens -= amount + + if self.tokens < 0: + time = -self.tokens/float(self.rate) + else: + time = 0 + + self.lock.release() - return allowable + return time - def drip(self): - if self.rate: + def calc_tokens(self): + if self.tokens < self.rate: now = time() - deltaT = now - self.lastDrip - self.content = long(max(0, self.content - deltaT * self.rate)) - self.lastDrip = now - else: - self.content = 0 + delta = self.rate * (now - self.timestamp) + self.tokens = min(self.rate, self.tokens + delta) + self.timestamp = now + |