mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-27 15:31:31 +00:00
lnbase: Peer handles its own disconnection instead of lnworker
This commit is contained in:
parent
f3dd7ce615
commit
707c7d569d
2 changed files with 17 additions and 7 deletions
|
@ -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:
|
||||
|
|
|
@ -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…
Add table
Reference in a new issue