Skip to content

Commit

Permalink
Performance tuning objects to be downloaded
Browse files Browse the repository at this point in the history
- rely on dict quasi-random order instead of an additional shuffle
- request an object once per minute
- stop check after count objects have been found
  • Loading branch information
PeterSurda committed Jan 15, 2017
1 parent dbe15d0 commit 79893fd
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/inventory.py
Expand Up @@ -106,23 +106,15 @@ def pull(self, count=1):
raise ValueError("Must be at least one")
with self.lock:
now = time.time()
since = now - 300 # once every 5 minutes
try:
matchingHashes = {k:v for k, v in self.hashes.iteritems() if current_thread().peer in self.hashes[k]['peers'] and self.hashes[k]['requested'] < since}
if count > len(matchingHashes):
count = len(matchingHashes)
objectHashes = random.sample(matchingHashes, count)
except (IndexError, KeyError): # list is empty
return None
for objectHash in objectHashes:
try:
since = now - 60 # once every minute
objectHashes = []
for objectHash in self.hashes.keys():
if current_thread().peer in self.hashes[objectHash]['peers'] and self.hashes[objectHash]['requested'] < since:
objectHashes.append(objectHash)
self.hashes[objectHash]['peers'].remove(current_thread().peer)
except ValueError:
pass
if len(self.hashes[objectHash]['peers']) == 0:
self.delete(objectHash)
else:
self.hashes[objectHash]['requested'] = now
if len(objectHashes) >= count:
break
return objectHashes

def delete(self, objectHash):
Expand Down

0 comments on commit 79893fd

Please sign in to comment.