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:
@@ -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