From 094092138380d5e11aa41037b8d1f1af77b944b8 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Mon, 26 Dec 2022 02:02:57 -0500 Subject: [PATCH 1/2] handle more client connection errors --- relay/http_client.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/relay/http_client.py b/relay/http_client.py index 8802471..9f28760 100644 --- a/relay/http_client.py +++ b/relay/http_client.py @@ -2,7 +2,8 @@ import logging import traceback from aiohttp import ClientSession, ClientTimeout, TCPConnector -from aiohttp.client_exceptions import ClientConnectorError, ServerTimeoutError +from aiohttp.client_exceptions import ClientConnectionError, ClientSSLError +from asyncio.exceptions import TimeoutError as AsyncTimeoutError from aputils import Nodeinfo, WellKnownNodeinfo from datetime import datetime from cachetools import LRUCache @@ -124,12 +125,14 @@ class HttpClient: except JSONDecodeError: logging.verbose(f'Failed to parse JSON') - except (ClientConnectorError, ServerTimeoutError): + except ClientSSLError: + logging.verbose(f'SSL error when connecting to {urlparse(url).netloc}') + + except (AsyncTimeoutError, ClientConnectionError): logging.verbose(f'Failed to connect to {urlparse(url).netloc}') except Exception as e: traceback.print_exc() - raise e async def post(self, url, message): @@ -158,8 +161,11 @@ class HttpClient: logging.verbose(f'Received error when pushing to {url}: {resp.status}') return logging.verbose(await resp.read()) # change this to debug - except (ClientConnectorError, ServerTimeoutError): - logging.verbose(f'Failed to connect to {url}') + except ClientSSLError: + logging.warning(f'SSL error when pushing to {urlparse(url).netloc}') + + except (AsyncTimeoutError, ClientConnectionError): + logging.warning(f'Failed to connect to {urlparse(url).netloc} for message push') ## prevent workers from being brought down except Exception as e: From f4698aa4dc339c14325aede556b162c8c1f539bd Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Thu, 29 Dec 2022 07:27:35 -0500 Subject: [PATCH 2/2] fix RuntimeError when running commands involving http client --- relay/http_client.py | 24 ++++++++++++++++++++++++ relay/manage.py | 13 +++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/relay/http_client.py b/relay/http_client.py index 9f28760..81fcd46 100644 --- a/relay/http_client.py +++ b/relay/http_client.py @@ -38,6 +38,15 @@ class HttpClient: self._session = None + async def __aenter__(self): + await self.open() + return self + + + async def __aexit__(self, *_): + await self.close() + + @property def limit(self): return self.cfg['limit'] @@ -196,3 +205,18 @@ class HttpClient: return False return await self.get(nodeinfo_url, loads=Nodeinfo.new_from_json) or False + + +async def get(database, *args, **kwargs): + async with HttpClient(database) as client: + return await client.get(*args, **kwargs) + + +async def post(database, *args, **kwargs): + async with HttpClient(database) as client: + return await client.post(*args, **kwargs) + + +async def fetch_nodeinfo(database, *args, **kwargs): + async with HttpClient(database) as client: + return await client.fetch_nodeinfo(*args, **kwargs) diff --git a/relay/manage.py b/relay/manage.py index 0d7decc..c36f876 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -7,6 +7,7 @@ import platform from urllib.parse import urlparse from . import misc, __version__ +from . import http_client as http from .application import Application from .config import RELAY_SOFTWARE @@ -148,7 +149,7 @@ def cli_inbox_follow(actor): inbox = inbox_data['inbox'] except KeyError: - actor_data = asyncio.run(app.client.get(actor, sign_headers=True)) + actor_data = asyncio.run(http.get(app.database, actor, sign_headers=True)) if not actor_data: return click.echo(f'Failed to fetch actor: {actor}') @@ -160,7 +161,7 @@ def cli_inbox_follow(actor): actor = actor ) - asyncio.run(app.client.post(inbox, message)) + asyncio.run(http.post(app.database, inbox, message)) click.echo(f'Sent follow message to actor: {actor}') @@ -186,7 +187,7 @@ def cli_inbox_unfollow(actor): ) except KeyError: - actor_data = asyncio.run(app.client.get(actor, sign_headers=True)) + actor_data = asyncio.run(http.get(app.database, actor, sign_headers=True)) inbox = actor_data.shared_inbox message = misc.Message.new_unfollow( host = app.config.host, @@ -198,7 +199,7 @@ def cli_inbox_unfollow(actor): } ) - asyncio.run(app.client.post(inbox, message)) + asyncio.run(http.post(app.database, inbox, message)) click.echo(f'Sent unfollow message to: {actor}') @@ -322,7 +323,7 @@ def cli_software_ban(name, fetch_nodeinfo): return click.echo('Banned all relay software') if fetch_nodeinfo: - nodeinfo = asyncio.run(app.client.fetch_nodeinfo(name)) + nodeinfo = asyncio.run(http.fetch_nodeinfo(app.database, name)) if not nodeinfo: click.echo(f'Failed to fetch software name from domain: {name}') @@ -352,7 +353,7 @@ def cli_software_unban(name, fetch_nodeinfo): return click.echo('Unbanned all relay software') if fetch_nodeinfo: - nodeinfo = asyncio.run(app.client.fetch_nodeinfo(name)) + nodeinfo = asyncio.run(http.fetch_nodeinfo(app.database, name)) if not nodeinfo: click.echo(f'Failed to fetch software name from domain: {name}')