Przeglądaj źródła

De-dupe list of retryable proxy exceptions

Nick Barrett 2 lat temu
rodzic
commit
a481b076ad
3 zmienionych plików z 31 dodań i 20 usunięć
  1. 1 1
      mauigpapi/__init__.py
  2. 21 0
      mauigpapi/proxy.py
  3. 9 19
      mautrix_instagram/user.py

+ 1 - 1
mauigpapi/__init__.py

@@ -1,4 +1,4 @@
 from .http import AndroidAPI
 from .mqtt import AndroidMQTT, GraphQLSubscription, SkywalkerSubscription
-from .proxy import ProxyHandler
+from .proxy import RETRYABLE_PROXY_EXCEPTIONS, ProxyHandler
 from .state import AndroidState

+ 21 - 0
mauigpapi/proxy.py

@@ -1,9 +1,30 @@
 from __future__ import annotations
 
+import asyncio
 import json
 import logging
 import urllib.request
 
+from aiohttp import ClientConnectionError
+
+try:
+    from aiohttp_socks import ProxyConnectionError, ProxyError, ProxyTimeoutError
+except ImportError:
+
+    class ProxyError(Exception):
+        pass
+
+    ProxyConnectionError = ProxyTimeoutError = ProxyError
+
+RETRYABLE_PROXY_EXCEPTIONS = (
+    ProxyError,
+    ProxyTimeoutError,
+    ProxyConnectionError,
+    ClientConnectionError,
+    ConnectionError,
+    asyncio.TimeoutError,
+)
+
 
 class ProxyHandler:
     current_proxy_url: str | None = None

+ 9 - 19
mautrix_instagram/user.py

@@ -22,9 +22,13 @@ import asyncio
 import logging
 import time
 
-from aiohttp import ClientConnectionError
-
-from mauigpapi import AndroidAPI, AndroidMQTT, AndroidState, ProxyHandler
+from mauigpapi import (
+    RETRYABLE_PROXY_EXCEPTIONS,
+    AndroidAPI,
+    AndroidMQTT,
+    AndroidState,
+    ProxyHandler,
+)
 from mauigpapi.errors import (
     IGChallengeError,
     IGCheckpointError,
@@ -653,14 +657,7 @@ class User(DBUser, BaseUser):
             try:
                 resp = await self.client.get_inbox()
                 break
-            except (
-                ProxyError,
-                ProxyTimeoutError,
-                ProxyConnectionError,
-                ClientConnectionError,
-                ConnectionError,
-                asyncio.TimeoutError,
-            ) as e:
+            except RETRYABLE_PROXY_EXCEPTIONS as e:
                 errors += 1
                 wait = min(errors * 10, 60)
                 self.log.warning(
@@ -875,14 +872,7 @@ class User(DBUser, BaseUser):
         while True:
             try:
                 resp = await self.client.current_user()
-            except (
-                ProxyError,
-                ProxyTimeoutError,
-                ProxyConnectionError,
-                ClientConnectionError,
-                ConnectionError,
-                asyncio.TimeoutError,
-            ) as e:
+            except RETRYABLE_PROXY_EXCEPTIONS as e:
                 errors += 1
                 wait = min(errors * 10, 60)
                 self.log.warning(