chore: 暂存本地修改
This commit is contained in:
@@ -14,6 +14,7 @@ from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import fcntl
|
||||
from request.proxy_config import is_proxy_enabled
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
@@ -167,13 +168,27 @@ global_rate_limiter = RateLimiter(
|
||||
)
|
||||
|
||||
|
||||
def _should_limit_proxy_requests() -> bool:
|
||||
"""
|
||||
仅在当前进程实际启用代理时启用全局代理限流。
|
||||
"""
|
||||
try:
|
||||
return is_proxy_enabled()
|
||||
except Exception:
|
||||
return True
|
||||
|
||||
|
||||
def wait_for_request():
|
||||
"""等待直到可以发起请求。"""
|
||||
if not _should_limit_proxy_requests():
|
||||
return
|
||||
global_rate_limiter.acquire()
|
||||
|
||||
|
||||
def can_request_now() -> bool:
|
||||
"""检查是否可以立即发起请求。"""
|
||||
if not _should_limit_proxy_requests():
|
||||
return True
|
||||
return global_rate_limiter.can_make_request()
|
||||
|
||||
|
||||
@@ -184,6 +199,10 @@ def request_slot():
|
||||
|
||||
这样既能限制“每秒启动多少请求”,也能限制“同时在飞多少请求”。
|
||||
"""
|
||||
if not _should_limit_proxy_requests():
|
||||
yield
|
||||
return
|
||||
|
||||
token = global_rate_limiter.try_acquire_slot()
|
||||
try:
|
||||
yield
|
||||
|
||||
Reference in New Issue
Block a user