mirror of
https://github.com/langgenius/dify.git
synced 2026-11-10 21:35:45 +08:00
feat(api): cache remote recommended app template fetches (#40176)
This commit is contained in:
1 parent
e9eb130731
commit
81a38df814
3 files changed
+144
-12
No files matched your search
@@ -451,6 +451,12 @@ class HostedFetchAppTemplateConfig(BaseSettings):
|
||||
default="https://tmpl.dify.ai",
|
||||
)
|
||||
|
||||
HOSTED_FETCH_APP_TEMPLATES_CACHE_TTL: int = Field(
|
||||
description="TTL in seconds for caching remote app template HTTP responses. 0 disables caching.",
|
||||
default=600,
|
||||
ge=0,
|
||||
)
|
||||
|
||||
|
||||
class HostedFetchPipelineTemplateConfig(BaseSettings):
|
||||
"""
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import logging
|
||||
import threading
|
||||
from typing import Any, override
|
||||
|
||||
import httpx
|
||||
from cachetools import TTLCache
|
||||
from flask import has_request_context, request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -13,6 +15,11 @@ from services.recommend_app.recommend_app_type import RecommendAppType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_REMOTE_FETCH_CACHE_MAXSIZE = 64
|
||||
_remote_fetch_cache: TTLCache[tuple[str, str], dict[str, Any]] | None = None
|
||||
_remote_fetch_cache_ttl: int | None = None
|
||||
_remote_fetch_cache_lock = threading.Lock()
|
||||
|
||||
|
||||
def _current_origin_headers() -> dict[str, str]:
|
||||
origin = request.headers.get("Origin") if has_request_context() else None
|
||||
@@ -25,6 +32,61 @@ def _current_origin_headers() -> dict[str, str]:
|
||||
return {"Origin": console_web_url}
|
||||
|
||||
|
||||
def _remote_fetch_cache_key(url: str, headers: dict[str, str]) -> tuple[str, str]:
|
||||
return url, headers.get("Origin", "")
|
||||
|
||||
|
||||
def _hosted_fetch_cache_ttl() -> int:
|
||||
ttl = dify_config.HOSTED_FETCH_APP_TEMPLATES_CACHE_TTL
|
||||
if isinstance(ttl, int) and not isinstance(ttl, bool):
|
||||
return ttl
|
||||
return 600
|
||||
|
||||
|
||||
def _get_remote_fetch_cache() -> TTLCache[tuple[str, str], dict[str, Any]] | None:
|
||||
ttl = _hosted_fetch_cache_ttl()
|
||||
if ttl <= 0:
|
||||
return None
|
||||
|
||||
global _remote_fetch_cache, _remote_fetch_cache_ttl
|
||||
if _remote_fetch_cache is None or _remote_fetch_cache_ttl != ttl:
|
||||
with _remote_fetch_cache_lock:
|
||||
if _remote_fetch_cache is None or _remote_fetch_cache_ttl != ttl:
|
||||
_remote_fetch_cache = TTLCache(maxsize=_REMOTE_FETCH_CACHE_MAXSIZE, ttl=ttl)
|
||||
_remote_fetch_cache_ttl = ttl
|
||||
return _remote_fetch_cache
|
||||
|
||||
|
||||
def clear_remote_fetch_cache() -> None:
|
||||
"""Reset the in-memory remote fetch cache (used by tests)."""
|
||||
global _remote_fetch_cache, _remote_fetch_cache_ttl
|
||||
with _remote_fetch_cache_lock:
|
||||
_remote_fetch_cache = None
|
||||
_remote_fetch_cache_ttl = None
|
||||
|
||||
|
||||
def _fetch_remote_payload(url: str) -> tuple[int, dict[str, Any] | None]:
|
||||
headers = _current_origin_headers()
|
||||
cache_key = _remote_fetch_cache_key(url, headers)
|
||||
cache = _get_remote_fetch_cache()
|
||||
if cache is not None:
|
||||
with _remote_fetch_cache_lock:
|
||||
cached = cache.get(cache_key)
|
||||
if cached is not None:
|
||||
return 200, cached
|
||||
|
||||
response = httpx.get(url, headers=headers, timeout=httpx.Timeout(10.0, connect=3.0))
|
||||
status_code = response.status_code
|
||||
if status_code != 200:
|
||||
return status_code, None
|
||||
|
||||
result: dict[str, Any] = response.json()
|
||||
if cache is not None:
|
||||
with _remote_fetch_cache_lock:
|
||||
cache[cache_key] = result
|
||||
return status_code, result
|
||||
|
||||
|
||||
class RemoteRecommendAppRetrieval(RecommendAppRetrievalBase):
|
||||
"""
|
||||
Retrieval recommended app from dify official.
|
||||
@@ -75,10 +137,9 @@ class RemoteRecommendAppRetrieval(RecommendAppRetrievalBase):
|
||||
"""
|
||||
domain = dify_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN
|
||||
url = f"{domain}/apps/{app_id}"
|
||||
response = httpx.get(url, headers=_current_origin_headers(), timeout=httpx.Timeout(10.0, connect=3.0))
|
||||
if response.status_code != 200:
|
||||
status_code, data = _fetch_remote_payload(url)
|
||||
if status_code != 200:
|
||||
return None
|
||||
data: dict[str, Any] = response.json()
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
@@ -90,11 +151,10 @@ class RemoteRecommendAppRetrieval(RecommendAppRetrievalBase):
|
||||
"""
|
||||
domain = dify_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN
|
||||
url = f"{domain}/apps?language={language}"
|
||||
response = httpx.get(url, headers=_current_origin_headers(), timeout=httpx.Timeout(10.0, connect=3.0))
|
||||
if response.status_code != 200:
|
||||
raise ValueError(f"fetch recommended apps failed, status code: {response.status_code}")
|
||||
status_code, result = _fetch_remote_payload(url)
|
||||
if status_code != 200:
|
||||
raise ValueError(f"fetch recommended apps failed, status code: {status_code}")
|
||||
|
||||
result: dict[str, Any] = response.json()
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
@@ -106,9 +166,8 @@ class RemoteRecommendAppRetrieval(RecommendAppRetrievalBase):
|
||||
"""
|
||||
domain = dify_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN
|
||||
url = f"{domain}/apps/learn-dify?language={language}"
|
||||
response = httpx.get(url, headers=_current_origin_headers(), timeout=httpx.Timeout(10.0, connect=3.0))
|
||||
if response.status_code != 200:
|
||||
raise ValueError(f"fetch learn dify apps failed, status code: {response.status_code}")
|
||||
status_code, result = _fetch_remote_payload(url)
|
||||
if status_code != 200:
|
||||
raise ValueError(f"fetch learn dify apps failed, status code: {status_code}")
|
||||
|
||||
result: dict[str, Any] = response.json()
|
||||
return result
|
||||
@@ -7,7 +7,14 @@ from sqlalchemy import Engine
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from services.recommend_app.recommend_app_type import RecommendAppType
|
||||
from services.recommend_app.remote.remote_retrieval import RemoteRecommendAppRetrieval
|
||||
from services.recommend_app.remote.remote_retrieval import RemoteRecommendAppRetrieval, clear_remote_fetch_cache
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clear_remote_fetch_cache_between_tests():
|
||||
clear_remote_fetch_cache()
|
||||
yield
|
||||
clear_remote_fetch_cache()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -249,3 +256,63 @@ class TestFetchFromDifyOfficial:
|
||||
|
||||
with pytest.raises(ValueError, match="fetch learn dify apps failed"):
|
||||
RemoteRecommendAppRetrieval.fetch_learn_dify_apps_from_dify_official("en-US")
|
||||
|
||||
@patch("services.recommend_app.remote.remote_retrieval.dify_config")
|
||||
@patch("services.recommend_app.remote.remote_retrieval.httpx.get")
|
||||
def test_apps_uses_cache_for_repeated_requests(self, mock_get, mock_config):
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN = "https://example.com"
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_CACHE_TTL = 600
|
||||
mock_response = MagicMock(status_code=200)
|
||||
mock_response.json.return_value = {"recommended_apps": [{"id": "app-1"}]}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
first = RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
second = RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
|
||||
assert first == second == {"recommended_apps": [{"id": "app-1"}]}
|
||||
mock_get.assert_called_once()
|
||||
|
||||
@patch("services.recommend_app.remote.remote_retrieval.dify_config")
|
||||
@patch("services.recommend_app.remote.remote_retrieval.httpx.get")
|
||||
def test_apps_does_not_cache_failed_responses(self, mock_get, mock_config):
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN = "https://example.com"
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_CACHE_TTL = 600
|
||||
mock_get.return_value = MagicMock(status_code=500)
|
||||
|
||||
with pytest.raises(ValueError, match="fetch recommended apps failed"):
|
||||
RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
with pytest.raises(ValueError, match="fetch recommended apps failed"):
|
||||
RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
|
||||
assert mock_get.call_count == 2
|
||||
|
||||
@patch("services.recommend_app.remote.remote_retrieval.dify_config")
|
||||
@patch("services.recommend_app.remote.remote_retrieval.httpx.get")
|
||||
def test_apps_skips_cache_when_ttl_disabled(self, mock_get, mock_config):
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN = "https://example.com"
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_CACHE_TTL = 0
|
||||
mock_response = MagicMock(status_code=200)
|
||||
mock_response.json.return_value = {"recommended_apps": []}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
|
||||
assert mock_get.call_count == 2
|
||||
|
||||
@patch("services.recommend_app.remote.remote_retrieval.dify_config")
|
||||
@patch("services.recommend_app.remote.remote_retrieval.httpx.get")
|
||||
def test_apps_cache_isolated_by_origin_header(self, mock_get, mock_config):
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN = "https://example.com"
|
||||
mock_config.HOSTED_FETCH_APP_TEMPLATES_CACHE_TTL = 600
|
||||
mock_response = MagicMock(status_code=200)
|
||||
mock_response.json.return_value = {"recommended_apps": []}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
flask_app = Flask(__name__)
|
||||
with flask_app.test_request_context(headers={"Origin": "https://cloud-a.example.com"}):
|
||||
RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
with flask_app.test_request_context(headers={"Origin": "https://cloud-b.example.com"}):
|
||||
RemoteRecommendAppRetrieval.fetch_recommended_apps_from_dify_official("en-US")
|
||||
|
||||
assert mock_get.call_count == 2
|
||||
Reference in new issue
Block a user