feat: add image upstream deadline
This commit is contained in:
@@ -8,6 +8,8 @@ from typing import Any, Iterable
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from .base import AiProviderConfigError, AiResponseParseError
|
||||
|
||||
@@ -26,6 +28,7 @@ SUPPORTED_API_TYPES = {
|
||||
}
|
||||
|
||||
RESOLUTION_TIMEOUTS = {"512": 180, "1K": 240, "2K": 360, "4K": 600}
|
||||
DEFAULT_IMAGE_UPSTREAM_DEADLINE_SECONDS = 180
|
||||
BASE64_KEYS = {"image_base64", "base64", "b64_json", "data"}
|
||||
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".gif"}
|
||||
|
||||
@@ -44,6 +47,34 @@ def request_timeout(connect_timeout: int, read_timeout: int, resolution: str) ->
|
||||
return connect_timeout, resolved_read_timeout
|
||||
|
||||
|
||||
def image_request_timeout(connect_timeout: int, read_timeout: int, resolution: str) -> tuple[int, int]:
|
||||
resolved_connect_timeout, resolved_read_timeout = request_timeout(
|
||||
connect_timeout,
|
||||
read_timeout,
|
||||
resolution,
|
||||
)
|
||||
return resolved_connect_timeout, cap_image_read_timeout(resolved_read_timeout)
|
||||
|
||||
|
||||
def cap_image_read_timeout(read_timeout: int) -> int:
|
||||
deadline = image_upstream_deadline_seconds()
|
||||
if deadline <= 0:
|
||||
return read_timeout
|
||||
return min(read_timeout, deadline)
|
||||
|
||||
|
||||
def image_upstream_deadline_seconds() -> int:
|
||||
try:
|
||||
value = getattr(
|
||||
settings,
|
||||
"AI_IMAGE_UPSTREAM_DEADLINE_SECONDS",
|
||||
DEFAULT_IMAGE_UPSTREAM_DEADLINE_SECONDS,
|
||||
)
|
||||
except ImproperlyConfigured:
|
||||
value = DEFAULT_IMAGE_UPSTREAM_DEADLINE_SECONDS
|
||||
return int(value)
|
||||
|
||||
|
||||
def detect_api_type(url: str, api_type: str = API_AUTO) -> str:
|
||||
if api_type and api_type != API_AUTO:
|
||||
if api_type not in SUPPORTED_API_TYPES:
|
||||
|
||||
Reference in New Issue
Block a user