Browse Source

fix: connection timeout

Timothy J. Baek 11 months ago
parent
commit
014e52c072
2 changed files with 4 additions and 2 deletions
  1. 2 1
      backend/apps/ollama/main.py
  2. 2 1
      backend/apps/openai/main.py

+ 2 - 1
backend/apps/ollama/main.py

@@ -124,8 +124,9 @@ async def cancel_ollama_request(request_id: str, user=Depends(get_current_user))
 
 
 async def fetch_url(url):
+    timeout = aiohttp.ClientTimeout(total=5)
     try:
-        async with aiohttp.ClientSession() as session:
+        async with aiohttp.ClientSession(timeout=timeout) as session:
             async with session.get(url) as response:
                 return await response.json()
     except Exception as e:

+ 2 - 1
backend/apps/openai/main.py

@@ -182,10 +182,11 @@ async def speech(request: Request, user=Depends(get_verified_user)):
 
 
 async def fetch_url(url, key):
+    timeout = aiohttp.ClientTimeout(total=5)
     try:
         if key != "":
             headers = {"Authorization": f"Bearer {key}"}
-            async with aiohttp.ClientSession() as session:
+            async with aiohttp.ClientSession(timeout=timeout) as session:
                 async with session.get(url, headers=headers) as response:
                     return await response.json()
         else: