mirror of
https://github.com/Flowseal/tg-ws-proxy.git
synced 2026-11-23 16:06:32 +03:00
CF worker pool: включить cooldown упавших доменов (#1633)
This commit is contained in:
+22
-6
@@ -237,6 +237,7 @@ class _CfWorkerPool:
|
||||
PER_DC_LIMIT = 1
|
||||
REFILL_BACKOFF_INITIAL = 1.0
|
||||
REFILL_BACKOFF_MAX = 3600.0
|
||||
FAILURE_COOLDOWN = 60.0
|
||||
|
||||
def __init__(self):
|
||||
self._idle: Dict[int, deque] = {}
|
||||
@@ -404,17 +405,32 @@ class _CfWorkerPool:
|
||||
return domains
|
||||
|
||||
def report_failure(self, worker_domain: str, exc: Exception) -> None:
|
||||
return # TODO: check status code after daily limit reached
|
||||
if not isinstance(exc, WsHandshakeError) or exc.status_code != 429:
|
||||
return
|
||||
|
||||
now = time.time()
|
||||
if self._exhausted_until.get(worker_domain, 0) > now:
|
||||
return
|
||||
exhausted_until = now + (86400 - (now % 86400))
|
||||
|
||||
if isinstance(exc, WsHandshakeError):
|
||||
if exc.status_code == 429:
|
||||
# Workers free plan reports the daily request limit as 429.
|
||||
exhausted_until = now + (86400 - (now % 86400))
|
||||
reason = 'daily request limit'
|
||||
elif exc.status_code >= 500:
|
||||
exhausted_until = now + self.FAILURE_COOLDOWN
|
||||
reason = 'HTTP %d' % exc.status_code
|
||||
else:
|
||||
# A challenge or a misconfigured route is not a reason to
|
||||
# disable the whole domain.
|
||||
return
|
||||
elif isinstance(exc, (OSError, asyncio.TimeoutError)):
|
||||
exhausted_until = now + self.FAILURE_COOLDOWN
|
||||
reason = repr(exc)
|
||||
else:
|
||||
return
|
||||
|
||||
self._exhausted_until[worker_domain] = exhausted_until
|
||||
log.warning(
|
||||
"CF worker %s reached its request limit, disabled for %d seconds", worker_domain, int(exhausted_until - now))
|
||||
"CF worker %s disabled for %d seconds: %s",
|
||||
worker_domain, max(1, int(exhausted_until - now)), reason)
|
||||
|
||||
async def _quiet_close(self, ws):
|
||||
try:
|
||||
|
||||
@@ -418,5 +418,69 @@ class CfWorkerPoolTest(unittest.IsolatedAsyncioTestCase):
|
||||
idle.writer.close.assert_called_once()
|
||||
|
||||
|
||||
class CfWorkerFailureReportTest(unittest.TestCase):
|
||||
NOW = 1_700_000_000.0
|
||||
|
||||
def _report(self, exc, now=None):
|
||||
pool = _CfWorkerPool()
|
||||
with mock.patch('proxy.pool.time.time', return_value=now or self.NOW):
|
||||
pool.report_failure('w.example', exc)
|
||||
return pool
|
||||
|
||||
def _available(self, pool, now):
|
||||
with mock.patch('proxy.pool.time.time', return_value=now):
|
||||
return pool.available_domains(['w.example'])
|
||||
|
||||
def test_429_disables_domain_until_the_next_day(self):
|
||||
pool = self._report(WsHandshakeError(429, '429 Too Many Requests'))
|
||||
until = pool._exhausted_until['w.example']
|
||||
self.assertGreater(until, self.NOW)
|
||||
self.assertLessEqual(until, self.NOW + 86400)
|
||||
self.assertEqual(self._available(pool, self.NOW), [])
|
||||
self.assertEqual(self._available(pool, until + 1), ['w.example'])
|
||||
|
||||
def test_5xx_pauses_domain_for_a_minute(self):
|
||||
pool = self._report(WsHandshakeError(503, '503 Service Unavailable'))
|
||||
self.assertEqual(
|
||||
pool._exhausted_until['w.example'],
|
||||
self.NOW + _CfWorkerPool.FAILURE_COOLDOWN,
|
||||
)
|
||||
self.assertEqual(self._available(pool, self.NOW), [])
|
||||
self.assertEqual(
|
||||
self._available(
|
||||
pool, self.NOW + _CfWorkerPool.FAILURE_COOLDOWN + 1),
|
||||
['w.example'],
|
||||
)
|
||||
|
||||
def test_network_errors_pause_domain(self):
|
||||
for exc in (OSError('connection reset'), asyncio.TimeoutError()):
|
||||
with self.subTest(exc=exc):
|
||||
pool = self._report(exc)
|
||||
self.assertEqual(self._available(pool, self.NOW), [])
|
||||
self.assertEqual(
|
||||
self._available(
|
||||
pool, self.NOW + _CfWorkerPool.FAILURE_COOLDOWN + 1),
|
||||
['w.example'],
|
||||
)
|
||||
|
||||
def test_client_4xx_keeps_domain_available(self):
|
||||
for status in (400, 403, 404):
|
||||
with self.subTest(status=status):
|
||||
pool = self._report(WsHandshakeError(status, '%d error' % status))
|
||||
self.assertNotIn('w.example', pool._exhausted_until)
|
||||
self.assertEqual(self._available(pool, self.NOW), ['w.example'])
|
||||
|
||||
def test_unknown_errors_are_ignored(self):
|
||||
pool = self._report(ValueError('unexpected'))
|
||||
self.assertNotIn('w.example', pool._exhausted_until)
|
||||
|
||||
def test_longer_quota_pause_is_not_shortened(self):
|
||||
pool = self._report(WsHandshakeError(429, '429 Too Many Requests'))
|
||||
until = pool._exhausted_until['w.example']
|
||||
with mock.patch('proxy.pool.time.time', return_value=self.NOW + 1):
|
||||
pool.report_failure('w.example', WsHandshakeError(503, '503 error'))
|
||||
self.assertEqual(pool._exhausted_until['w.example'], until)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user