objectracker pylint fixes

This commit is contained in:
lakshyacis 2019-09-06 15:58:06 +05:30
parent db1593f428
commit fa6ef4f933
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB

View File

@ -1,3 +1,7 @@
"""
src/network/objectracker.py
===========================
"""
import time import time
from threading import RLock from threading import RLock
@ -27,6 +31,7 @@ missingObjects = {}
class ObjectTracker(object): class ObjectTracker(object):
"""Object tracker mixin"""
invCleanPeriod = 300 invCleanPeriod = 300
invInitialCapacity = 50000 invInitialCapacity = 50000
invErrorRate = 0.03 invErrorRate = 0.03
@ -42,21 +47,24 @@ class ObjectTracker(object):
self.lastCleaned = time.time() self.lastCleaned = time.time()
def initInvBloom(self): def initInvBloom(self):
"""Init bloom filter for tracking. WIP."""
if haveBloom: if haveBloom:
# lock? # lock?
self.invBloom = BloomFilter(capacity=ObjectTracker.invInitialCapacity, self.invBloom = BloomFilter(capacity=ObjectTracker.invInitialCapacity,
error_rate=ObjectTracker.invErrorRate) error_rate=ObjectTracker.invErrorRate)
def initAddrBloom(self): def initAddrBloom(self):
"""Init bloom filter for tracking addrs, WIP. This either needs to be moved to addrthread.py or removed."""
if haveBloom: if haveBloom:
# lock? # lock?
self.addrBloom = BloomFilter(capacity=ObjectTracker.invInitialCapacity, self.addrBloom = BloomFilter(capacity=ObjectTracker.invInitialCapacity,
error_rate=ObjectTracker.invErrorRate) error_rate=ObjectTracker.invErrorRate)
def clean(self): def clean(self):
"""Clean up tracking to prevent memory bloat"""
if self.lastCleaned < time.time() - ObjectTracker.invCleanPeriod: if self.lastCleaned < time.time() - ObjectTracker.invCleanPeriod:
if haveBloom: if haveBloom:
if len(missingObjects) == 0: if missingObjects == 0:
self.initInvBloom() self.initInvBloom()
self.initAddrBloom() self.initAddrBloom()
else: else:
@ -67,12 +75,13 @@ class ObjectTracker(object):
self.lastCleaned = time.time() self.lastCleaned = time.time()
def hasObj(self, hashid): def hasObj(self, hashid):
"""Do we already have object?"""
if haveBloom: if haveBloom:
return hashid in self.invBloom return hashid in self.invBloom
else:
return hashid in self.objectsNewToMe return hashid in self.objectsNewToMe
def handleReceivedInventory(self, hashId): def handleReceivedInventory(self, hashId):
"""Handling received inventory"""
if haveBloom: if haveBloom:
self.invBloom.add(hashId) self.invBloom.add(hashId)
try: try:
@ -85,6 +94,7 @@ class ObjectTracker(object):
self.objectsNewToMe[hashId] = True self.objectsNewToMe[hashId] = True
def handleReceivedObject(self, streamNumber, hashid): def handleReceivedObject(self, streamNumber, hashid):
"""Handling received object"""
for i in network.connectionpool.BMConnectionPool().inboundConnections.values( for i in network.connectionpool.BMConnectionPool().inboundConnections.values(
) + network.connectionpool.BMConnectionPool().outboundConnections.values(): ) + network.connectionpool.BMConnectionPool().outboundConnections.values():
if not i.fullyEstablished: if not i.fullyEstablished:
@ -109,25 +119,12 @@ class ObjectTracker(object):
self.objectsNewToMe.setLastObject() self.objectsNewToMe.setLastObject()
def hasAddr(self, addr): def hasAddr(self, addr):
"""WIP, should be moved to addrthread.py or removed"""
if haveBloom: if haveBloom:
return addr in self.invBloom return addr in self.invBloom
return None
def addAddr(self, hashid): def addAddr(self, hashid):
"""WIP, should be moved to addrthread.py or removed"""
if haveBloom: if haveBloom:
self.addrBloom.add(hashid) self.addrBloom.add(hashid)
"""
addr sending -> per node upload queue, and flush every minute or so
inv sending -> if not in bloom, inv immediately, otherwise put into a per node upload queue
and flush every minute or so
data sending -> a simple queue
no bloom
- if inv arrives
- if we don't have it, add tracking and download queue
- if we do have it, remove from tracking
tracking downloads
- per node hash of items the node has but we don't
tracking inv
- per node hash of items that neither the remote node nor we have
"""