Browse Source

lnbase: Peer handles its own disconnection instead of lnworker

regtest_lnd
SomberNight 6 years ago
parent
commit
bc8895cc36
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 19
      electrum/lnbase.py
  2. 5
      electrum/lnworker.py

19
electrum/lnbase.py

@ -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
@ -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:

5
electrum/lnworker.py

@ -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

Loading…
Cancel
Save