implement bandwidth throttling, fixes #189

This commit is contained in:
j 2016-01-18 21:15:59 +05:30
commit 9f3374a7dc
3 changed files with 76 additions and 27 deletions

View file

@ -312,7 +312,7 @@ class Node(Thread):
logger.debug('download %s', url)
self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values()))
try:
r = self._opener.open(url, timeout=self.TIMEOUT*2)
r = self._opener.open(url, timeout=self.TIMEOUT*5)
except:
logger.debug('openurl failed %s', url, exc_info=1)
return False
@ -324,7 +324,8 @@ class Node(Thread):
content = b''
ct = datetime.utcnow()
size = 0
for chunk in iter(lambda: fileobj.read(16*1024), b''):
chunk_size = 16*1024
for chunk in iter(lambda: fileobj.read(chunk_size), b''):
content += chunk
size += len(chunk)
since_ct = (datetime.utcnow() - ct).total_seconds()
@ -335,25 +336,18 @@ class Node(Thread):
# transfer was canceled
return False
else:
t.progress = len(content) / item.info['size']
t.progress = size / item.info['size']
t.save()
trigger_event('transfer', {
'id': item.id, 'progress': t.progress
})
if state.bandwidth:
state.bandwidth.download(size/since_ct)
size = 0
'''
content = fileobj.read()
'''
if state.bandwidth:
state.bandwidth.download(size/since_ct)
size = 0
if state.bandwidth:
while not state.bandwidth.download(chunk_size) and self._running:
time.sleep(0.1)
t2 = datetime.utcnow()
duration = (t2-t1).total_seconds()
if duration:
self.download_speed = len(content) / duration
self.download_speed = size / duration
return item.save_file(content)
except:
logger.debug('download failed %s', url, exc_info=1)