Fallback CF-worker to HTTP after TimeoutError

This commit is contained in:
Flowseal
2026-11-23 12:14:38 +03:00
parent f42468b77a
commit 4a338a0f5f
4 changed files with 79 additions and 44 deletions
+5 -25
View File
@@ -4,7 +4,6 @@ import struct
import time
from typing import Dict, List, Optional, Set, Tuple
from urllib.parse import urlencode
from .utils import *
from .stats import stats
@@ -203,31 +202,12 @@ async def _cfproxy_worker_fallback(reader, writer, relay_init, label,
log.info("[%s] DC%d%s -> CF worker pool hit via %s for %s",
label, dc, media_tag, worker_domain, fallback_dst)
else:
query = urlencode({
'dst': fallback_dst,
'dc': str(dc),
})
path = f'/apiws?{query}'
ws = None
for worker_domain in cf_worker_pool.available_domains(worker_domains):
log.info("[%s] DC%d%s -> trying CF worker %s for %s",
label, dc, media_tag, worker_domain, fallback_dst)
try:
with client_setup(reader, writer):
ws = await RawWebSocket.connect(worker_domain, worker_domain,
timeout=10.0, path=path,
secure=not proxy_config.disable_secure)
break
except Exception as exc:
cf_worker_pool.report_failure(worker_domain, exc)
log.warning("[%s] DC%d%s CF worker %s failed: %s",
label, dc, media_tag, worker_domain, repr(exc))
continue
if ws is None:
with client_setup(reader, writer):
connected = await cf_worker_pool.connect_one(worker_domains, fallback_dst, dc, timeout=10.0)
if connected is None:
return False
ws, worker_domain = connected
log.info("[%s] DC%d%s -> CF worker %s for %s", label, dc, media_tag, worker_domain, fallback_dst)
stats.connections_cfproxy += 1
await ws.send_init(relay_init)
+18 -10
View File
@@ -246,6 +246,7 @@ class _CfWorkerPool:
self._refill_failures: Dict[int, int] = {}
self._refill_after: Dict[int, float] = {}
self._exhausted_until: Dict[str, float] = {}
self.try_secure_first = True
async def get(self, dc: int, fallback_dst: str,
worker_domains: List[str]
@@ -309,7 +310,7 @@ class _CfWorkerPool:
return
connected = 0
for _ in range(needed):
result = await self._connect_one(
result = await self.connect_one(
worker_domains, fallback_dst, dc)
if self._refilling.get(dc) is not asyncio.current_task():
if result is not None:
@@ -384,22 +385,28 @@ class _CfWorkerPool:
if self._rotating.get(dc) is asyncio.current_task():
self._rotating.pop(dc, None)
async def _connect_one(self, worker_domains, fallback_dst, dc):
async def connect_one(self, worker_domains: List[str], fallback_dst: str, dc: int, *,
timeout: float = 8.0) -> Optional[Tuple[RawWebSocket, str]]:
query = urlencode({
'dst': fallback_dst,
'dc': str(dc),
})
path = f'/apiws?{query}'
modes = (False,) if proxy_config.disable_secure else (self.try_secure_first, not self.try_secure_first)
for worker_domain in self.available_domains(worker_domains):
try:
ws = await RawWebSocket.connect(
worker_domain, worker_domain, timeout=8, path=path,
secure=not proxy_config.disable_secure)
for attempt, secure in enumerate(modes, 1):
try:
ws = await RawWebSocket.connect(
worker_domain, worker_domain, timeout=timeout, path=path, secure=secure)
except Exception as exc:
log.debug("CF worker connect DC%d via %s for %s failed (tls=%s): %r",
dc, worker_domain, fallback_dst, secure, exc)
if attempt < len(modes) and isinstance(exc, (asyncio.TimeoutError, TimeoutError)):
continue
self.report_failure(worker_domain, exc)
break
self.try_secure_first = secure
return ws, worker_domain
except Exception as exc:
log.debug("CF worker pool connect DC%d via %s for %s failed: %r",
dc, worker_domain, fallback_dst, exc)
self.report_failure(worker_domain, exc)
return None
def available_domains(self, worker_domains: List[str]) -> List[str]:
@@ -484,6 +491,7 @@ class _CfWorkerPool:
self._refill_failures.clear()
self._refill_after.clear()
self._exhausted_until.clear()
self.try_secure_first = True
async def close(self):
tasks = list(self._rotating.values()) + list(self._refilling.values())