Skip to content

Commit b649494

Browse files
ApplEOFDiscordzjjliveinJiang-Jia-Jun
authored
[Feature] add HTTP GET retry (#3838)
* add http get retry * fix coments --------- Co-authored-by: zhangjunjun04 <zhangjunjun04@baidu.com> Co-authored-by: Jiang-Jia-Jun <163579578+Jiang-Jia-Jun@users.noreply.github.com>
1 parent 7c26869 commit b649494

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

fastdeploy/entrypoints/chat_utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616

1717
import os
18+
import time
1819
import uuid
1920
from copy import deepcopy
2021
from pathlib import Path
@@ -32,6 +33,7 @@
3233

3334
from fastdeploy.multimodal.image import ImageMediaIO
3435
from fastdeploy.multimodal.video import VideoMediaIO
36+
from fastdeploy.utils import api_server_logger
3537

3638

3739
class VideoURL(TypedDict, total=False):
@@ -90,12 +92,32 @@ def parse_video(self, video_url):
9092
"""Parse Video"""
9193
return self.load_from_url(video_url, self.video_io)
9294

95+
def http_get_with_retry(self, url, max_retries=3, retry_delay=1, backoff_factor=2):
96+
"""HTTP GET retry"""
97+
98+
retry_cnt = 0
99+
delay = retry_delay
100+
101+
while retry_cnt < max_retries:
102+
try:
103+
response = requests.get(url)
104+
response.raise_for_status()
105+
return response.content
106+
except Exception as e:
107+
retry_cnt += 1
108+
if retry_cnt >= max_retries:
109+
api_server_logger.error(f"HTTP GET failed: {e}. Max retries reached")
110+
raise
111+
api_server_logger.info(f"HTTP GET failed: {e}. Start retry {retry_cnt}")
112+
time.sleep(delay)
113+
delay *= backoff_factor
114+
93115
def load_from_url(self, url, media_io):
94116
"""Load media from URL"""
95117

96118
parsed = urlparse(url)
97119
if parsed.scheme.startswith("http"):
98-
media_bytes = requests.get(url).content
120+
media_bytes = self.http_get_with_retry(url)
99121
return media_io.load_bytes(media_bytes)
100122

101123
if parsed.scheme.startswith("data"):

0 commit comments

Comments
 (0)