2020-05-20 21:44:05 +02:00
|
|
|
import asyncio
|
2021-11-22 23:34:03 +01:00
|
|
|
|
|
|
|
from typing import Optional
|
2020-05-20 21:44:05 +02:00
|
|
|
|
2021-04-04 05:55:26 +02:00
|
|
|
from chia.server.server import ChiaServer
|
|
|
|
from chia.types.peer_info import PeerInfo
|
2021-11-22 23:34:03 +01:00
|
|
|
from chia.util.network import get_host_addr
|
2020-07-09 14:03:59 +02:00
|
|
|
|
|
|
|
|
2022-09-29 19:14:20 +02:00
|
|
|
def start_reconnect_task(server: ChiaServer, peer_info_arg: PeerInfo, log, prefer_ipv6: Optional[bool]):
|
2020-05-20 21:44:05 +02:00
|
|
|
"""
|
2020-06-03 06:13:01 +02:00
|
|
|
Start a background task that checks connection and reconnects periodically to a peer.
|
2020-05-20 21:44:05 +02:00
|
|
|
"""
|
2021-10-08 01:56:34 +02:00
|
|
|
# If peer_info_arg is already an address, use it, otherwise resolve it here.
|
|
|
|
if peer_info_arg.is_valid():
|
|
|
|
peer_info = peer_info_arg
|
|
|
|
else:
|
2021-11-22 23:34:03 +01:00
|
|
|
peer_info = PeerInfo(get_host_addr(peer_info_arg, prefer_ipv6), peer_info_arg.port)
|
2020-05-20 21:44:05 +02:00
|
|
|
|
|
|
|
async def connection_check():
|
|
|
|
while True:
|
|
|
|
peer_retry = True
|
2020-12-02 08:50:22 +01:00
|
|
|
for _, connection in server.all_connections.items():
|
|
|
|
if connection.get_peer_info() == peer_info or connection.get_peer_info() == peer_info_arg:
|
2020-05-20 21:44:05 +02:00
|
|
|
peer_retry = False
|
|
|
|
if peer_retry:
|
|
|
|
log.info(f"Reconnecting to peer {peer_info}")
|
2020-07-09 14:03:59 +02:00
|
|
|
try:
|
2022-09-29 19:14:20 +02:00
|
|
|
await server.start_client(peer_info, None)
|
2020-07-09 14:03:59 +02:00
|
|
|
except Exception as e:
|
2020-10-31 21:20:36 +01:00
|
|
|
log.info(f"Failed to connect to {peer_info} {e}")
|
2020-12-02 13:12:28 +01:00
|
|
|
await asyncio.sleep(3)
|
2020-05-20 21:44:05 +02:00
|
|
|
|
|
|
|
return asyncio.create_task(connection_check())
|