tlnbase: Peer handles its own disconnection instead of lnworker - electrum - Electrum Bitcoin wallet (HTM) git clone https://git.parazyd.org/electrum (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules --- (DIR) commit 707c7d569d25cef2092ee67145c7582b1ef0325d (DIR) parent f3dd7ce615072b814dda4f6c4378315fc1f13646 (HTM) Author: SomberNight <somber.night@protonmail.com> Date: Fri, 5 Oct 2018 16:33:35 +0200 lnbase: Peer handles its own disconnection instead of lnworker Diffstat: M electrum/lnbase.py | 19 +++++++++++++++++-- M electrum/lnworker.py | 5 ----- 2 files changed, 17 insertions(+), 7 deletions(-) --- (DIR) diff --git a/electrum/lnbase.py b/electrum/lnbase.py t@@ -267,7 +267,6 @@ def create_ephemeral_key() -> (bytes, bytes): class Peer(PrintError): def __init__(self, lnworker, host, port, pubkey, request_initial_sync=False): - self.exception = None # set by aiosafe self.host = host self.port = port self.pubkey = pubkey t@@ -461,9 +460,25 @@ class Peer(PrintError): self.process_message(msg) self.initialized.set_result(True) + def handle_disconnect(func): + async def wrapper_func(self, *args, **kwargs): + try: + return await func(self, *args, **kwargs) + except LightningPeerConnectionClosed as e: + self.print_error("disconnecting gracefully. {}".format(e)) + finally: + self.close_and_cleanup() + self.lnworker.peers.pop(self.pubkey) + return wrapper_func + @aiosafe + @handle_disconnect async def main_loop(self): - await asyncio.wait_for(self.initialize(), 5) + try: + await asyncio.wait_for(self.initialize(), 10) + except (OSError, asyncio.TimeoutError, HandshakeFailed) as e: + self.print_error('disconnecting due to: {}'.format(repr(e))) + return self.channel_db.add_recent_peer(self.peer_addr) # loop while True: (DIR) diff --git a/electrum/lnworker.py b/electrum/lnworker.py t@@ -403,11 +403,6 @@ class LNWorker(PrintError): while True: await asyncio.sleep(1) now = time.time() - for node_id, peer in list(self.peers.items()): - if peer.exception: - self.print_error("removing peer", peer.host) - peer.close_and_cleanup() - self.peers.pop(node_id) self.reestablish_peers_and_channels() if len(self.peers) >= NUM_PEERS_TARGET: continue