flake8 for knownnodes
This commit is contained in:
parent
74ddda8775
commit
ca42b4be63
|
@ -1,9 +1,9 @@
|
||||||
import pickle
|
|
||||||
import os
|
import os
|
||||||
|
import pickle
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from bmconfigparser import BMConfigParser
|
|
||||||
import state
|
import state
|
||||||
|
from bmconfigparser import BMConfigParser
|
||||||
|
|
||||||
knownNodesLock = threading.Lock()
|
knownNodesLock = threading.Lock()
|
||||||
knownNodes = {}
|
knownNodes = {}
|
||||||
|
@ -13,6 +13,7 @@ knownNodesTrimAmount = 2000
|
||||||
# forget a node after rating is this low
|
# forget a node after rating is this low
|
||||||
knownNodesForgetRating = -0.5
|
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
|
||||||
|
@ -20,30 +21,43 @@ def saveKnownNodes(dirName = None):
|
||||||
with open(os.path.join(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):
|
||||||
increaseAmount = 0.1
|
increaseAmount = 0.1
|
||||||
maxRating = 1
|
maxRating = 1
|
||||||
with knownNodesLock:
|
with knownNodesLock:
|
||||||
for stream in knownNodes.keys():
|
for stream in knownNodes.keys():
|
||||||
try:
|
try:
|
||||||
knownNodes[stream][peer]["rating"] = min(knownNodes[stream][peer]["rating"] + increaseAmount, maxRating)
|
knownNodes[stream][peer]["rating"] = min(
|
||||||
|
knownNodes[stream][peer]["rating"] + increaseAmount,
|
||||||
|
maxRating
|
||||||
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def decreaseRating(peer):
|
def decreaseRating(peer):
|
||||||
decreaseAmount = 0.1
|
decreaseAmount = 0.1
|
||||||
minRating = -1
|
minRating = -1
|
||||||
with knownNodesLock:
|
with knownNodesLock:
|
||||||
for stream in knownNodes.keys():
|
for stream in knownNodes.keys():
|
||||||
try:
|
try:
|
||||||
knownNodes[stream][peer]["rating"] = max(knownNodes[stream][peer]["rating"] - decreaseAmount, minRating)
|
knownNodes[stream][peer]["rating"] = max(
|
||||||
|
knownNodes[stream][peer]["rating"] - decreaseAmount,
|
||||||
|
minRating
|
||||||
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def trimKnownNodes(recAddrStream=1):
|
def trimKnownNodes(recAddrStream=1):
|
||||||
if len(knownNodes[recAddrStream]) < int(BMConfigParser().get("knownnodes", "maxnodes")):
|
if len(knownNodes[recAddrStream]) < \
|
||||||
|
BMConfigParser().safeGetInt("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]
|
||||||
for oldest in oldestList:
|
for oldest in oldestList:
|
||||||
del knownNodes[recAddrStream][oldest]
|
del knownNodes[recAddrStream][oldest]
|
||||||
|
|
Reference in New Issue
Block a user