Moved communicator shutdown over to KV model for more decoupling

This commit is contained in:
Kevin 2020-07-25 21:36:48 -05:00
parent 0460d3380f
commit 0e4e7bb050
8 changed files with 30 additions and 22 deletions

View file

@ -61,6 +61,7 @@ class OnionrCommunicatorDaemon:
# populate kv values
self.shared_state.get_by_string('DeadSimpleKV').put('blockQueue', {})
self.shared_state.get_by_string('DeadSimpleKV').put('shutdown', False)
if config.get('general.offline_mode', False):
self.isOnline = False
@ -97,9 +98,6 @@ class OnionrCommunicatorDaemon:
# amount of threads running by name, used to prevent too many
self.threadCounts = {}
# set true when shutdown command received
self.shutdown = False
# list of blocks currently downloading
self.currentDownloading = []
@ -239,23 +237,28 @@ class OnionrCommunicatorDaemon:
get_url()
while not config.get('onboarding.done', True) and \
not self.shutdown:
not self.shared_state.get_by_string(
'DeadSimpleKV').get('shutdown'):
try:
time.sleep(2)
except KeyboardInterrupt:
self.shutdown = True
self.shared_state.get_by_string(
'DeadSimpleKV').put('shutdown', True)
# Main daemon loop, mainly for calling timers,
# don't do any complex operations here to avoid locking
try:
while not self.shutdown:
while not self.shared_state.get_by_string(
'DeadSimpleKV').get('shutdown'):
for i in self.timers:
if self.shutdown:
if self.shared_state.get_by_string(
'DeadSimpleKV').get('shutdown'):
break
i.processTimer()
time.sleep(self.delay)
except KeyboardInterrupt:
self.shutdown = True
self.shared_state.get_by_string(
'DeadSimpleKV').put('shutdown', True)
logger.info(
'Goodbye. (Onionr is cleaning up, and will exit)', terminal=True)

View file

@ -31,6 +31,7 @@ def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
Connect to more peers if we have none connected
"""
config = comm_inst.config
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
if config.get('general.offline_mode', False):
comm_inst.decrementThreadCount('get_online_peers')
return
@ -49,7 +50,7 @@ def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
else:
comm_inst.connectNewPeer()
if comm_inst.shutdown:
if kv.get('shutdown'):
break
else:
if len(comm_inst.onlinePeers) == 0:

View file

@ -27,6 +27,7 @@ def peer_action(comm_inst, peer, action,
returnHeaders=False, max_resp_size=5242880):
"""Perform a get request to a peer."""
penalty_score = -10
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
if len(peer) == 0:
return False
url = 'http://%s/%s' % (peer, action)
@ -47,7 +48,7 @@ def peer_action(comm_inst, peer, action,
onlinepeers.remove_online_peer(comm_inst, peer)
keydb.transportinfo.set_address_info(
peer, 'lastConnectAttempt', epoch.get_epoch())
if action != 'ping' and not comm_inst.shutdown:
if action != 'ping' and not kv.get('shutdown'):
logger.warn(f'Lost connection to {peer}', terminal=True)
# Will only add a new peer to pool if needed
onlinepeers.get_online_peers(comm_inst)