forked from lanqian528/chat2api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.py
32 lines (27 loc) · 1.37 KB
/
retry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import HTTPException
from utils.Logger import logger
from utils.config import retry_times
async def async_retry(func, *args, max_retries=retry_times, **kwargs):
for attempt in range(max_retries + 1):
try:
result = await func(*args, **kwargs)
return result
except HTTPException as e:
if attempt == max_retries:
logger.error(f"Throw an exception {e.status_code}, {e.detail}")
if e.status_code == 500:
raise HTTPException(status_code=500, detail="Server error")
raise HTTPException(status_code=e.status_code, detail=e.detail)
logger.info(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")
def retry(func, *args, max_retries=retry_times, **kwargs):
for attempt in range(max_retries + 1):
try:
result = func(*args, **kwargs)
return result
except HTTPException as e:
if attempt == max_retries:
logger.error(f"Throw an exception {e.status_code}, {e.detail}")
if e.status_code == 500:
raise HTTPException(status_code=500, detail="Server error")
raise HTTPException(status_code=e.status_code, detail=e.detail)
logger.error(f"Retry {attempt + 1} status code {e.status_code}, {e.detail}. Retrying...")