Moved onlinePeers to KV to further reduce coupling

This commit is contained in:
Kevin 2020-07-25 22:28:32 -05:00
parent 0e4e7bb050
commit 6a6718c9fd
15 changed files with 56 additions and 25 deletions

View file

@ -60,8 +60,10 @@ class OnionrCommunicatorDaemon:
self.shared_state = shared_state # TooManyObjects module
# populate kv values
self.shared_state.get_by_string('DeadSimpleKV').put('blockQueue', {})
self.shared_state.get_by_string('DeadSimpleKV').put('shutdown', False)
self.kv = self.shared_state.get_by_string('DeadSimpleKV')
self.kv.put('blockQueue', {})
self.kv.put('shutdown', False)
self.kv.put('onlinePeers', [])
if config.get('general.offline_mode', False):
self.isOnline = False
@ -82,7 +84,6 @@ class OnionrCommunicatorDaemon:
self.delay = 1
# lists of connected peers and peers we know we can't reach currently
self.onlinePeers = []
self.offlinePeers = []
self.cooldownPeer = {}
self.connectTimes = {}

View file

@ -8,6 +8,7 @@ from typing import TYPE_CHECKING
from etc import humanreadabletime
import logger
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
from communicator import OnionrCommunicatorDaemon
"""
This program is free software: you can redistribute it and/or modify
@ -26,7 +27,7 @@ if TYPE_CHECKING:
def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
"""Manage the comm_inst.onlinePeers attribute list.
"""Manage the kv.get('onlinePeers') attribute list.
Connect to more peers if we have none connected
"""
@ -37,7 +38,7 @@ def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
return
logger.debug('Refreshing peer pool...')
max_peers = int(config.get('peers.max_connect', 10))
needed = max_peers - len(comm_inst.onlinePeers)
needed = max_peers - len(kv.get('onlinePeers'))
last_seen = 'never'
if not isinstance(comm_inst.lastNodeSeen, type(None)):
@ -45,7 +46,7 @@ def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
comm_inst.lastNodeSeen)
for _ in range(needed):
if len(comm_inst.onlinePeers) == 0:
if len(kv.get('onlinePeers')) == 0:
comm_inst.connectNewPeer(useBootstrap=True)
else:
comm_inst.connectNewPeer()
@ -53,7 +54,7 @@ def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
if kv.get('shutdown'):
break
else:
if len(comm_inst.onlinePeers) == 0:
if len(kv.get('onlinePeers')) == 0:
logger.debug('Couldn\'t connect to any peers.' +
f' Last node seen {last_seen} ago.')
try:

View file

@ -4,8 +4,12 @@ Onionr - Private P2P Communication.
pick online peers in a communicator instance
"""
import secrets
from typing import TYPE_CHECKING
import onionrexceptions
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -24,18 +28,19 @@ import onionrexceptions
def pick_online_peer(comm_inst):
"""Randomly picks peer from pool without bias (using secrets module)."""
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
ret_data = ''
peer_length = len(comm_inst.onlinePeers)
peer_length = len(kv.get('onlinePeers'))
if peer_length <= 0:
raise onionrexceptions.OnlinePeerNeeded
while True:
peer_length = len(comm_inst.onlinePeers)
peer_length = len(kv.get('onlinePeers'))
try:
# Get a random online peer, securely.
# May get stuck in loop if network is lost
ret_data = comm_inst.onlinePeers[secrets.randbelow(peer_length)]
ret_data = kv.get('onlinePeers')[secrets.randbelow(peer_length)]
except IndexError:
pass
else:

View file

@ -2,6 +2,10 @@
remove an online peer from the pool in a communicator instance
"""
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -20,6 +24,7 @@ remove an online peer from the pool in a communicator instance
def remove_online_peer(comm_inst, peer):
"""Remove an online peer."""
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
try:
del comm_inst.connectTimes[peer]
except KeyError:
@ -29,6 +34,6 @@ def remove_online_peer(comm_inst, peer):
except KeyError:
pass
try:
comm_inst.onlinePeers.remove(peer)
kv.get('onlinePeers').remove(peer)
except ValueError:
pass