mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-05 16:48:03 +08:00
[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>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
@@ -32,6 +33,7 @@ from typing_extensions import Required, TypeAlias, TypedDict
|
||||
|
||||
from fastdeploy.multimodal.image import ImageMediaIO
|
||||
from fastdeploy.multimodal.video import VideoMediaIO
|
||||
from fastdeploy.utils import api_server_logger
|
||||
|
||||
|
||||
class VideoURL(TypedDict, total=False):
|
||||
@@ -90,12 +92,32 @@ class MultiModalPartParser:
|
||||
"""Parse Video"""
|
||||
return self.load_from_url(video_url, self.video_io)
|
||||
|
||||
def http_get_with_retry(self, url, max_retries=3, retry_delay=1, backoff_factor=2):
|
||||
"""HTTP GET retry"""
|
||||
|
||||
retry_cnt = 0
|
||||
delay = retry_delay
|
||||
|
||||
while retry_cnt < max_retries:
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
return response.content
|
||||
except Exception as e:
|
||||
retry_cnt += 1
|
||||
if retry_cnt >= max_retries:
|
||||
api_server_logger.error(f"HTTP GET failed: {e}. Max retries reached")
|
||||
raise
|
||||
api_server_logger.info(f"HTTP GET failed: {e}. Start retry {retry_cnt}")
|
||||
time.sleep(delay)
|
||||
delay *= backoff_factor
|
||||
|
||||
def load_from_url(self, url, media_io):
|
||||
"""Load media from URL"""
|
||||
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme.startswith("http"):
|
||||
media_bytes = requests.get(url).content
|
||||
media_bytes = self.http_get_with_retry(url)
|
||||
return media_io.load_bytes(media_bytes)
|
||||
|
||||
if parsed.scheme.startswith("data"):
|
||||
|
Reference in New Issue
Block a user