2017-07-06 19:45:36 +02:00
|
|
|
import random
|
2017-06-21 12:16:33 +02:00
|
|
|
import threading
|
2017-07-05 09:25:49 +02:00
|
|
|
import time
|
2017-06-21 12:16:33 +02:00
|
|
|
|
|
|
|
import addresses
|
|
|
|
#from bmconfigparser import BMConfigParser
|
|
|
|
from debug import logger
|
|
|
|
from helper_threading import StoppableThread
|
|
|
|
#from inventory import Inventory
|
|
|
|
from network.connectionpool import BMConnectionPool
|
|
|
|
import protocol
|
|
|
|
|
|
|
|
class DownloadThread(threading.Thread, StoppableThread):
|
2017-07-05 09:25:49 +02:00
|
|
|
maxPending = 50
|
|
|
|
requestChunk = 100
|
|
|
|
requestTimeout = 60
|
|
|
|
cleanInterval = 60
|
|
|
|
requestExpires = 600
|
2017-06-21 12:16:33 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
threading.Thread.__init__(self, name="DownloadThread")
|
|
|
|
self.initStop()
|
|
|
|
self.name = "DownloadThread"
|
|
|
|
logger.info("init download thread")
|
2017-07-05 09:25:49 +02:00
|
|
|
self.pending = {}
|
|
|
|
self.lastCleaned = time.time()
|
|
|
|
|
|
|
|
def cleanPending(self):
|
|
|
|
deadline = time.time() - DownloadThread.requestExpires
|
|
|
|
self.pending = {k: v for k, v in self.pending.iteritems() if v >= deadline}
|
|
|
|
self.lastCleaned = time.time()
|
2017-06-21 12:16:33 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self._stopped:
|
|
|
|
requested = 0
|
2017-07-06 19:45:36 +02:00
|
|
|
# Choose downloading peers randomly
|
|
|
|
connections = BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values()
|
|
|
|
random.shuffle(connections)
|
|
|
|
for i in connections:
|
2017-07-05 09:25:49 +02:00
|
|
|
now = time.time()
|
|
|
|
timedOut = now - DownloadThread.requestTimeout
|
|
|
|
# this may take a while, but it needs a consistency so I think it's better to lock a bigger chunk
|
2017-06-21 12:16:33 +02:00
|
|
|
with i.objectsNewToMeLock:
|
2017-07-05 09:25:49 +02:00
|
|
|
downloadPending = len(list((k for k, v in i.objectsNewToMe.iteritems() if k in self.pending and self.pending[k] > timedOut)))
|
2017-06-21 12:16:33 +02:00
|
|
|
if downloadPending >= DownloadThread.maxPending:
|
|
|
|
continue
|
|
|
|
# keys with True values in the dict
|
2017-07-05 09:25:49 +02:00
|
|
|
request = list((k for k, v in i.objectsNewToMe.iteritems() if k not in self.pending or self.pending[k] < timedOut))
|
2017-06-24 12:13:35 +02:00
|
|
|
if not request:
|
2017-06-21 12:16:33 +02:00
|
|
|
continue
|
|
|
|
if len(request) > DownloadThread.requestChunk - downloadPending:
|
|
|
|
request = request[:DownloadThread.requestChunk - downloadPending]
|
|
|
|
# mark them as pending
|
|
|
|
for k in request:
|
|
|
|
i.objectsNewToMe[k] = False
|
2017-07-05 09:25:49 +02:00
|
|
|
self.pending[k] = now
|
2017-06-21 12:16:33 +02:00
|
|
|
|
|
|
|
payload = addresses.encodeVarint(len(request)) + ''.join(request)
|
2017-07-06 19:45:36 +02:00
|
|
|
i.append_write_buf(protocol.CreatePacket('getdata', payload))
|
2017-06-21 12:16:33 +02:00
|
|
|
logger.debug("%s:%i Requesting %i objects", i.destination.host, i.destination.port, len(request))
|
|
|
|
requested += len(request)
|
2017-07-05 09:25:49 +02:00
|
|
|
if time.time() >= self.lastCleaned + DownloadThread.cleanInterval:
|
|
|
|
self.cleanPending()
|
|
|
|
if not requested:
|
|
|
|
self.stop.wait(1)
|