Introduce outages - a dict of all hosts

to track multiport configurations / port change events
This commit is contained in:
Dmitri Bogomolov 2019-12-02 10:57:29 +02:00
parent 7b8bf082ff
commit 7558f74ab6
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -32,6 +32,9 @@ knownNodesForgetRating = -0.5
knownNodesActual = False knownNodesActual = False
outages = {}
"""a dict with all hosts"""
logger = logging.getLogger('default') logger = logging.getLogger('default')
DEFAULT_NODES = ( DEFAULT_NODES = (
@ -69,7 +72,13 @@ def json_deserialize_knownnodes(source):
for node in json.load(source): for node in json.load(source):
peer = node['peer'] peer = node['peer']
info = node['info'] info = node['info']
peer = Peer(str(peer['host']), peer.get('port', 8444)) port = peer.get('port', 8444)
peer = Peer(str(peer['host']), port)
outages[peer.host] = {
'lastseen': info.get('lastseen', time.time()),
'port': port,
'stream': info.get('stream', 1)
}
knownNodes[node['stream']][peer] = info knownNodes[node['stream']][peer] = info
if not (knownNodesActual if not (knownNodesActual
or info.get('self')) and peer not in DEFAULT_NODES: or info.get('self')) and peer not in DEFAULT_NODES:
@ -119,11 +128,15 @@ def addKnownNode(stream, peer, lastseen=None, is_self=False):
else: else:
lastseen = int(lastseen) lastseen = int(lastseen)
try: try:
prev = outages[peer.host]
if peer.port == prev['port']:
outages[peer.host]['lastseen'] = lastseen
info = knownNodes[stream].get(peer) info = knownNodes[stream].get(peer)
if lastseen > info['lastseen']: info['lastseen'] = lastseen
info['lastseen'] = lastseen except KeyError:
except (KeyError, TypeError):
pass pass
except TypeError:
rating = -0.2
else: else:
return return
@ -131,6 +144,22 @@ def addKnownNode(stream, peer, lastseen=None, is_self=False):
if len(knownNodes[stream]) > BMConfigParser().safeGetInt( if len(knownNodes[stream]) > BMConfigParser().safeGetInt(
"knownnodes", "maxnodes"): "knownnodes", "maxnodes"):
return return
try:
prev = outages[peer.host]
except KeyError:
outages[peer.host] = {
'stream': stream,
'port': peer.port,
'lastseen': lastseen
}
else:
if stream == prev['stream']:
if lastseen - prev['lastseen'] > 3600 * 24:
# more than a day ago, this should be port change
del knownNodes[stream][Peer(peer.host, prev['port'])]
outages[peer.host]['port'] = peer.port
else:
rating = -0.2
knownNodes[stream][peer] = { knownNodes[stream][peer] = {
'lastseen': lastseen, 'lastseen': lastseen,