Spaces:
Running
Running
Deploy 1643aa79
Browse files
backend/integrations/cloudflare_transport.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
| 3 |
|
| 4 |
import asyncio
|
| 5 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
| 6 |
import socket
|
| 7 |
import ssl
|
| 8 |
import threading
|
|
@@ -12,6 +13,7 @@ import httpx
|
|
| 12 |
|
| 13 |
|
| 14 |
HOST = "api.cloudflare.com"
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
class ReachableCloudflareRoutes:
|
|
@@ -44,6 +46,7 @@ class ReachableCloudflareRoutes:
|
|
| 44 |
routes = [route for future in as_completed(futures) if (route := future.result())]
|
| 45 |
self._routes = routes
|
| 46 |
self._expires = time.monotonic() + 60
|
|
|
|
| 47 |
if not routes:
|
| 48 |
raise httpx.ConnectError("No verified Cloudflare IPv4 route was reachable")
|
| 49 |
return routes[0]
|
|
@@ -72,7 +75,13 @@ class CloudflareTransport(httpx.BaseTransport):
|
|
| 72 |
def handle_request(self, request):
|
| 73 |
excluded = []
|
| 74 |
for attempt in range(3):
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
return self.transport.handle_request(_routed_request(request, address))
|
| 78 |
except (httpx.ConnectError, httpx.ConnectTimeout):
|
|
@@ -93,7 +102,13 @@ class AsyncCloudflareTransport(httpx.AsyncBaseTransport):
|
|
| 93 |
async def handle_async_request(self, request):
|
| 94 |
excluded = []
|
| 95 |
for attempt in range(3):
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
return await self.transport.handle_async_request(_routed_request(request, address))
|
| 99 |
except (httpx.ConnectError, httpx.ConnectTimeout):
|
|
|
|
| 3 |
|
| 4 |
import asyncio
|
| 5 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 6 |
+
import logging
|
| 7 |
import socket
|
| 8 |
import ssl
|
| 9 |
import threading
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
HOST = "api.cloudflare.com"
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
|
| 19 |
class ReachableCloudflareRoutes:
|
|
|
|
| 46 |
routes = [route for future in as_completed(futures) if (route := future.result())]
|
| 47 |
self._routes = routes
|
| 48 |
self._expires = time.monotonic() + 60
|
| 49 |
+
logger.info("Cloudflare route check: %d candidates, %d verified", len(addresses), len(routes))
|
| 50 |
if not routes:
|
| 51 |
raise httpx.ConnectError("No verified Cloudflare IPv4 route was reachable")
|
| 52 |
return routes[0]
|
|
|
|
| 75 |
def handle_request(self, request):
|
| 76 |
excluded = []
|
| 77 |
for attempt in range(3):
|
| 78 |
+
try:
|
| 79 |
+
address = self.routes.select(excluded)
|
| 80 |
+
except httpx.ConnectError:
|
| 81 |
+
if attempt == 2:
|
| 82 |
+
raise
|
| 83 |
+
time.sleep(2)
|
| 84 |
+
continue
|
| 85 |
try:
|
| 86 |
return self.transport.handle_request(_routed_request(request, address))
|
| 87 |
except (httpx.ConnectError, httpx.ConnectTimeout):
|
|
|
|
| 102 |
async def handle_async_request(self, request):
|
| 103 |
excluded = []
|
| 104 |
for attempt in range(3):
|
| 105 |
+
try:
|
| 106 |
+
address = await asyncio.to_thread(self.routes.select, excluded)
|
| 107 |
+
except httpx.ConnectError:
|
| 108 |
+
if attempt == 2:
|
| 109 |
+
raise
|
| 110 |
+
await asyncio.sleep(2)
|
| 111 |
+
continue
|
| 112 |
try:
|
| 113 |
return await self.transport.handle_async_request(_routed_request(request, address))
|
| 114 |
except (httpx.ConnectError, httpx.ConnectTimeout):
|
tests/test_cloudflare_provider.py
CHANGED
|
@@ -65,3 +65,22 @@ def test_transport_tries_another_route_only_for_connect_failure():
|
|
| 65 |
response = client.post('https://api.cloudflare.com/example', json={})
|
| 66 |
assert response.status_code == 200
|
| 67 |
assert routes.invalidated == ['192.0.2.1']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
response = client.post('https://api.cloudflare.com/example', json={})
|
| 66 |
assert response.status_code == 200
|
| 67 |
assert routes.invalidated == ['192.0.2.1']
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def test_transport_rechecks_when_no_route_is_temporarily_reachable(monkeypatch):
|
| 71 |
+
monkeypatch.setattr('backend.integrations.cloudflare_transport.time.sleep', lambda _: None)
|
| 72 |
+
class Routes:
|
| 73 |
+
calls = 0
|
| 74 |
+
def select(self, excluded):
|
| 75 |
+
self.calls += 1
|
| 76 |
+
if self.calls == 1:
|
| 77 |
+
raise httpx.ConnectError('No route')
|
| 78 |
+
return '192.0.2.1'
|
| 79 |
+
def invalidate(self, address):
|
| 80 |
+
raise AssertionError('reachable route should not be invalidated')
|
| 81 |
+
routes = Routes()
|
| 82 |
+
transport = CloudflareTransport(routes)
|
| 83 |
+
transport.transport = httpx.MockTransport(lambda request: httpx.Response(200))
|
| 84 |
+
with httpx.Client(transport=transport) as client:
|
| 85 |
+
assert client.get('https://api.cloudflare.com/example').status_code == 200
|
| 86 |
+
assert routes.calls == 2
|