From eac435b23344eb4120ddc4e8d232f127b4db396b Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 5 Jun 2025 22:13:33 +0800 Subject: [PATCH] fix lint --- app/actions/send_message.py | 2 +- app/api/endpoints/discover.py | 6 ++-- app/core/cache.py | 4 +-- app/helper/doh.py | 57 +++++++++++++++++------------------ 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/app/actions/send_message.py b/app/actions/send_message.py index 0a99fd9c..78d7117d 100644 --- a/app/actions/send_message.py +++ b/app/actions/send_message.py @@ -4,7 +4,7 @@ from pydantic import Field from app.actions import BaseAction, ActionChain from app.schemas import ActionParams, ActionContext, Notification -from core.config import settings +from app.core.config import settings class SendMessageParams(ActionParams): diff --git a/app/api/endpoints/discover.py b/app/api/endpoints/discover.py index c1eb0d5d..1f7117c7 100644 --- a/app/api/endpoints/discover.py +++ b/app/api/endpoints/discover.py @@ -7,9 +7,9 @@ from app.core.event import eventmanager from app.core.security import verify_token from app.schemas import DiscoverSourceEventData from app.schemas.types import ChainEventType, MediaType -from chain.bangumi import BangumiChain -from chain.douban import DoubanChain -from chain.tmdb import TmdbChain +from app.chain.bangumi import BangumiChain +from app.chain.douban import DoubanChain +from app.chain.tmdb import TmdbChain router = APIRouter() diff --git a/app/core/cache.py b/app/core/cache.py index c35bccb3..547e580c 100644 --- a/app/core/cache.py +++ b/app/core/cache.py @@ -196,7 +196,7 @@ class CacheToolsBackend(CacheBackend): return None return region_cache.get(key) - def delete(self, key: str, region: Optional[str] = DEFAULT_CACHE_REGION) -> None: + def delete(self, key: str, region: Optional[str] = DEFAULT_CACHE_REGION): """ 删除缓存 @@ -205,7 +205,7 @@ class CacheToolsBackend(CacheBackend): """ region_cache = self.__get_region_cache(region) if region_cache is None: - return None + return with lock: del region_cache[key] diff --git a/app/helper/doh.py b/app/helper/doh.py index f922c9a0..e730d70b 100644 --- a/app/helper/doh.py +++ b/app/helper/doh.py @@ -22,39 +22,36 @@ _executor = concurrent.futures.ThreadPoolExecutor() _doh_timeout = 5 _doh_cache: Dict[str, str] = {} - -def _patched_getaddrinfo(host, *args, **kwargs): - """ - socket.getaddrinfo的补丁版本。 - """ - if host not in settings.DOH_DOMAINS.split(","): - return _orig_getaddrinfo(host, *args, **kwargs) - - # 检查主机是否已解析 - if host in _doh_cache: - ip = _doh_cache[host] - logger.info("已解析 [%s] 为 [%s] (缓存)", host, ip) - return _orig_getaddrinfo(ip, *args, **kwargs) - - # 使用DoH解析主机 - futures = [] - for resolver in settings.DOH_RESOLVERS.split(","): - futures.append(_executor.submit(_doh_query, resolver, host)) - - for future in concurrent.futures.as_completed(futures): - ip = future.result() - if ip is not None: - logger.info("已解析 [%s] 为 [%s]", host, ip) - _doh_cache[host] = ip - host = ip - break - - return _orig_getaddrinfo(host, *args, **kwargs) - - # 对 socket.getaddrinfo 进行补丁 if settings.DOH_ENABLE: + # 保存原始的 socket.getaddrinfo 方法 _orig_getaddrinfo = socket.getaddrinfo + + def _patched_getaddrinfo(host, *args, **kwargs): + """ + socket.getaddrinfo的补丁版本。 + """ + if host not in settings.DOH_DOMAINS.split(","): + return _orig_getaddrinfo(host, *args, **kwargs) + # 检查主机是否已解析 + if host in _doh_cache: + ip = _doh_cache[host] + logger.info("已解析 [%s] 为 [%s] (缓存)", host, ip) + return _orig_getaddrinfo(ip, *args, **kwargs) + # 使用DoH解析主机 + futures = [] + for resolver in settings.DOH_RESOLVERS.split(","): + futures.append(_executor.submit(_doh_query, resolver, host)) + for future in concurrent.futures.as_completed(futures): + ip = future.result() + if ip is not None: + logger.info("已解析 [%s] 为 [%s]", host, ip) + _doh_cache[host] = ip + host = ip + break + return _orig_getaddrinfo(host, *args, **kwargs) + + # 替换 socket.getaddrinfo 方法 socket.getaddrinfo = _patched_getaddrinfo