Forget known nodes with bad rating

This commit is contained in:
Peter Šurda 2017-10-19 08:52:44 +02:00
parent 453bcdbb1d
commit d44c6c6464
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
3 changed files with 23 additions and 8 deletions

View File

@ -96,16 +96,24 @@ class singleCleaner(threading.Thread, StoppableThread):
# cleanup old nodes # cleanup old nodes
now = int(time.time()) now = int(time.time())
toDelete = []
with knownnodes.knownNodesLock: with knownnodes.knownNodesLock:
for stream in knownnodes.knownNodes: for stream in knownnodes.knownNodes:
for node in knownnodes.knownNodes[stream].keys(): keys = knownnodes.knownNodes[stream].keys()
for node in keys:
try: try:
# scrap old nodes
if now - knownnodes.knownNodes[stream][node]["lastseen"] > 2419200: # 28 days if now - knownnodes.knownNodes[stream][node]["lastseen"] > 2419200: # 28 days
shared.needToWriteKownNodesToDisk = True shared.needToWriteKnownNodesToDisk = True
del knownnodes.knownNodes[stream][node] del knownnodes.knownNodes[stream][node]
continue
# scrap old nodes with low rating
if now - knownnodes.knownNodes[stream][node]["lastseen"] > 10800 and knownnodes.knownNodes[stream][node]["rating"] <= knownnodes.knownNodesForgetRating:
shared.needToWriteKnownNodesToDisk = True
del knownnodes.knownNodes[stream][node]
continue
except TypeError: except TypeError:
print "Error in %s" % (str(node)) print "Error in %s" % (str(node))
keys = []
# Let us write out the knowNodes to disk if there is anything new to write out. # Let us write out the knowNodes to disk if there is anything new to write out.
if shared.needToWriteKnownNodesToDisk: if shared.needToWriteKnownNodesToDisk:

View File

@ -1,4 +1,5 @@
import pickle import pickle
import os
import threading import threading
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
@ -9,11 +10,14 @@ knownNodes = {}
knownNodesTrimAmount = 2000 knownNodesTrimAmount = 2000
# forget a node after rating is this low
knownNodesForgetRating = -0.5
def saveKnownNodes(dirName = None): def saveKnownNodes(dirName = None):
if dirName is None: if dirName is None:
dirName = state.appdata dirName = state.appdata
with knownNodesLock: with knownNodesLock:
with open(dirName + 'knownnodes.dat', 'wb') as output: with open(os.path.join(dirName, 'knownnodes.dat'), 'wb') as output:
pickle.dump(knownNodes, output) pickle.dump(knownNodes, output)
def increaseRating(peer): def increaseRating(peer):
@ -37,7 +41,7 @@ def decreaseRating(peer):
pass pass
def trimKnownNodes(recAddrStream = 1): def trimKnownNodes(recAddrStream = 1):
if len(knownNodes[recAddrStream]) < BMConfigParser().get("knownnodes", "maxnodes"): if len(knownNodes[recAddrStream]) < int(BMConfigParser().get("knownnodes", "maxnodes")):
return return
with knownNodesLock: with knownNodesLock:
oldestList = sorted(knownNodes[recAddrStream], key=lambda x: x['lastseen'])[:knownNodesTrimAmount] oldestList = sorted(knownNodes[recAddrStream], key=lambda x: x['lastseen'])[:knownNodesTrimAmount]

View File

@ -378,9 +378,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
continue continue
if decodedIP is not False and seenTime > time.time() - BMProto.addressAlive: if decodedIP is not False and seenTime > time.time() - BMProto.addressAlive:
peer = state.Peer(decodedIP, port) peer = state.Peer(decodedIP, port)
if peer in knownnodes.knownNodes[stream] and knownnodes.knownNodes[stream][peer]["lastseen"] > seenTime: try:
if knownnodes.knownNodes[stream][peer]["lastseen"] > seenTime:
continue continue
if len(knownnodes.knownNodes[stream]) < BMConfigParser().get("knownnodes", "maxnodes"): except KeyError:
pass
if len(knownnodes.knownNodes[stream]) < int(BMConfigParser().get("knownnodes", "maxnodes")):
with knownnodes.knownNodesLock: with knownnodes.knownNodesLock:
try: try:
knownnodes.knownNodes[stream][peer]["lastseen"] = seenTime knownnodes.knownNodes[stream][peer]["lastseen"] = seenTime