Add HTTP retry mechanism and request timeout to transcribe_legacy
Use requests Session with Retry adapter (5 retries, backoff factor 1) and add 30s timeout to both submit and query requests for better reliability against transient network errors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,8 @@ import time
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from requests.adapters import HTTPAdapter
|
||||||
|
from urllib3.util.retry import Retry
|
||||||
from boto3 import client as s3_client
|
from boto3 import client as s3_client
|
||||||
from botocore.config import Config
|
from botocore.config import Config
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@@ -22,6 +24,11 @@ S3_BUCKET = os.getenv("S3_BUCKET")
|
|||||||
SUBMIT_URL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/submit"
|
SUBMIT_URL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/submit"
|
||||||
QUERY_URL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/query"
|
QUERY_URL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/query"
|
||||||
|
|
||||||
|
retry = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
||||||
|
session = requests.Session()
|
||||||
|
session.mount("http://", HTTPAdapter(max_retries=retry))
|
||||||
|
session.mount("https://", HTTPAdapter(max_retries=retry))
|
||||||
|
|
||||||
s3 = s3_client(
|
s3 = s3_client(
|
||||||
"s3",
|
"s3",
|
||||||
endpoint_url=S3_ENDPOINT,
|
endpoint_url=S3_ENDPOINT,
|
||||||
@@ -116,7 +123,7 @@ body = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = requests.post(SUBMIT_URL, json=body, headers=headers)
|
resp = session.post(SUBMIT_URL, json=body, headers=headers, timeout=30)
|
||||||
logid = resp.headers.get("X-Tt-Logid", "")
|
logid = resp.headers.get("X-Tt-Logid", "")
|
||||||
status = resp.headers.get("X-Api-Status-Code", "")
|
status = resp.headers.get("X-Api-Status-Code", "")
|
||||||
print(f"Submit: {status}")
|
print(f"Submit: {status}")
|
||||||
@@ -125,13 +132,14 @@ if status != "20000000":
|
|||||||
print(f"FAILED: {resp.headers.get('X-Api-Message', '')}")
|
print(f"FAILED: {resp.headers.get('X-Api-Message', '')}")
|
||||||
else:
|
else:
|
||||||
while True:
|
while True:
|
||||||
resp = requests.post(
|
resp = session.post(
|
||||||
QUERY_URL,
|
QUERY_URL,
|
||||||
json={},
|
json={},
|
||||||
headers={
|
headers={
|
||||||
**make_headers(task_id),
|
**make_headers(task_id),
|
||||||
"X-Tt-Logid": logid,
|
"X-Tt-Logid": logid,
|
||||||
},
|
},
|
||||||
|
timeout=30,
|
||||||
)
|
)
|
||||||
code = resp.headers.get("X-Api-Status-Code", "")
|
code = resp.headers.get("X-Api-Status-Code", "")
|
||||||
if code == "20000000":
|
if code == "20000000":
|
||||||
|
|||||||
Reference in New Issue
Block a user