mirror of
https://github.com/NoCLin/LightMirrors
synced 2025-06-17 09:25:25 +08:00
feat: direct_proxy with retry
This commit is contained in:
parent
e6e2eae600
commit
c9ebbac425
@ -6,6 +6,7 @@ import httpx
|
||||
from httpx import Request as HttpxRequest
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
from tenacity import retry, retry_if_exception_type, stop_after_attempt
|
||||
|
||||
SyncPreProcessor = Callable[[Request, HttpxRequest], HttpxRequest]
|
||||
|
||||
@ -54,6 +55,14 @@ async def post_process_response(
|
||||
return response
|
||||
|
||||
|
||||
@retry(
|
||||
stop=stop_after_attempt(6),
|
||||
retry=retry_if_exception_type(Exception),
|
||||
reraise=True,
|
||||
after=lambda retry_state: logger.warning(
|
||||
f"retry {retry_state.attempt_number} {retry_state.outcome}"
|
||||
),
|
||||
)
|
||||
async def direct_proxy(
|
||||
request: Request,
|
||||
target_url: str,
|
||||
@ -74,6 +83,7 @@ async def direct_proxy(
|
||||
request.method,
|
||||
target_url,
|
||||
headers=req_headers,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
httpx_req = await pre_process_request(request, httpx_req, pre_process)
|
||||
|
19
src/poetry.lock
generated
19
src/poetry.lock
generated
@ -1,4 +1,4 @@
|
||||
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
|
||||
# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "annotated-types"
|
||||
@ -498,6 +498,21 @@ typing-extensions = {version = ">=3.10.0", markers = "python_version < \"3.10\""
|
||||
[package.extras]
|
||||
full = ["httpx (>=0.22.0)", "itsdangerous", "jinja2", "python-multipart", "pyyaml"]
|
||||
|
||||
[[package]]
|
||||
name = "tenacity"
|
||||
version = "8.4.2"
|
||||
description = "Retry code until it succeeds"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "tenacity-8.4.2-py3-none-any.whl", hash = "sha256:9e6f7cf7da729125c7437222f8a522279751cdfbe6b67bfe64f75d3a348661b2"},
|
||||
{file = "tenacity-8.4.2.tar.gz", hash = "sha256:cd80a53a79336edba8489e767f729e4f391c896956b57140b5d7511a64bbd3ef"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
doc = ["reno", "sphinx"]
|
||||
test = ["pytest", "tornado (>=4.5)", "typeguard"]
|
||||
|
||||
[[package]]
|
||||
name = "tomli"
|
||||
version = "2.0.1"
|
||||
@ -542,4 +557,4 @@ standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)",
|
||||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = "^3.9"
|
||||
content-hash = "95fe42214f7987d04a55af59e60128b0a3e224b1b2abbdf962053703379a7160"
|
||||
content-hash = "5e3dd2b39fd601cef175cb5768d11ea71f4583d815cbe8d97e4b2214d0ee1e97"
|
||||
|
@ -10,6 +10,7 @@ python = "^3.9"
|
||||
fastapi = "^0.109.0"
|
||||
uvicorn = "^0.27.0.post1"
|
||||
httpx = "^0.26.0"
|
||||
tenacity = "^8.4.2"
|
||||
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
|
@ -120,6 +120,9 @@ sniffio==1.3.0 ; python_version >= "3.9" and python_version < "4.0" \
|
||||
starlette==0.35.1 ; python_version >= "3.9" and python_version < "4.0" \
|
||||
--hash=sha256:3e2639dac3520e4f58734ed22553f950d3f3cb1001cd2eaac4d57e8cdc5f66bc \
|
||||
--hash=sha256:50bbbda9baa098e361f398fda0928062abbaf1f54f4fadcbe17c092a01eb9a25
|
||||
tenacity==8.4.2 ; python_version >= "3.9" and python_version < "4.0" \
|
||||
--hash=sha256:9e6f7cf7da729125c7437222f8a522279751cdfbe6b67bfe64f75d3a348661b2 \
|
||||
--hash=sha256:cd80a53a79336edba8489e767f729e4f391c896956b57140b5d7511a64bbd3ef
|
||||
typing-extensions==4.9.0 ; python_version >= "3.9" and python_version < "4.0" \
|
||||
--hash=sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783 \
|
||||
--hash=sha256:af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd
|
||||
|
Loading…
x
Reference in New Issue
Block a user