Browse Source

Use lower timeout for media download reqs

Nick Barrett 2 years ago
parent
commit
00b26d2a33
2 changed files with 11 additions and 2 deletions
  1. 2 1
      mauigpapi/http/base.py
  2. 9 1
      mautrix_instagram/portal.py

+ 2 - 1
mauigpapi/http/base.py

@@ -166,7 +166,7 @@ class BaseAndroidAPI:
         self.http = ClientSession(connector=connector, cookie_jar=cookie_jar)
         return None
 
-    def raw_http_get(self, url: URL | str):
+    def raw_http_get(self, url: URL | str, **kwargs):
         if isinstance(url, str):
             url = URL(url, encoded=True)
         return self.http.get(
@@ -175,6 +175,7 @@ class BaseAndroidAPI:
                 "user-agent": self.state.user_agent,
                 "accept-language": self.state.device.language.replace("_", "-"),
             },
+            **kwargs,
         )
 
     async def std_http_post(

+ 9 - 1
mautrix_instagram/portal.py

@@ -28,6 +28,7 @@ import re
 import sqlite3
 import time
 
+from aiohttp import ClientTimeout
 from yarl import URL
 import asyncpg
 import magic
@@ -869,8 +870,15 @@ class Portal(DBPortal, BasePortal):
     async def _download_instagram_file(
         self, source: u.User, url: str
     ) -> tuple[Optional[bytes], str]:
+        # Use a low timeout for media reqs so we quickly retry them if they get stuck
+        timeout = ClientTimeout(
+            total=30,
+            sock_connect=5,
+            sock_read=10,
+        )
+
         async def download():
-            async with source.client.raw_http_get(url) as resp:
+            async with source.client.raw_http_get(url, timeout=timeout) as resp:
                 try:
                     length = int(resp.headers["Content-Length"])
                 except KeyError: