|
38 | 38 | from .lib.browser_routing.routing import ( |
39 | 39 | BrowserRouteCache, |
40 | 40 | BrowserRoutingConfig, |
41 | | - strip_direct_vm_auth, |
| 41 | + prepare_direct_vm_request, |
42 | 42 | rewrite_direct_vm_options, |
43 | 43 | browser_routing_config_from_env, |
| 44 | + install_direct_vm_auth_stripping, |
| 45 | + is_stale_direct_vm_auth_response, |
44 | 46 | should_retry_stale_direct_vm_auth, |
| 47 | + install_stale_direct_vm_auth_eviction, |
| 48 | + install_async_direct_vm_auth_stripping, |
45 | 49 | maybe_evict_browser_route_from_response, |
| 50 | + should_retry_direct_vm_connection_error, |
| 51 | + install_async_stale_direct_vm_auth_eviction, |
| 52 | + direct_vm_request_body_is_known_unreplayable, |
46 | 53 | maybe_populate_browser_route_cache_from_response, |
47 | 54 | ) |
48 | 55 |
|
@@ -205,6 +212,8 @@ def __init__( |
205 | 212 | ) |
206 | 213 | self.browser_route_cache = _browser_route_cache or BrowserRouteCache() |
207 | 214 | self._browser_routing = browser_routing_config_from_env() |
| 215 | + install_direct_vm_auth_stripping(self._client) |
| 216 | + install_stale_direct_vm_auth_eviction(self._client) |
208 | 217 |
|
209 | 218 | @cached_property |
210 | 219 | def deployments(self) -> DeploymentsResource: |
@@ -369,13 +378,21 @@ def _prepare_options(self, options: Any) -> Any: |
369 | 378 |
|
370 | 379 | @override |
371 | 380 | def _prepare_request(self, request: httpx.Request) -> None: |
372 | | - strip_direct_vm_auth(request, cache=self.browser_route_cache) |
| 381 | + prepare_direct_vm_request(request, cache=self.browser_route_cache) |
| 382 | + |
| 383 | + @override |
| 384 | + def _should_retry_on_connection_error(self, request: httpx.Request) -> bool: |
| 385 | + return should_retry_direct_vm_connection_error(request) |
373 | 386 |
|
374 | 387 | @override |
375 | 388 | def _should_retry(self, response: httpx.Response) -> bool: |
376 | | - if should_retry_stale_direct_vm_auth(response): |
377 | | - maybe_evict_browser_route_from_response(response, cache=self.browser_route_cache) |
378 | | - return True |
| 389 | + if direct_vm_request_body_is_known_unreplayable(response.request): |
| 390 | + return False |
| 391 | + if is_stale_direct_vm_auth_response(response): |
| 392 | + # The route was already evicted by the response hook; retry only when |
| 393 | + # the body can be rebuilt, otherwise the caller sees the original auth |
| 394 | + # failure and a later call goes to the control plane. |
| 395 | + return should_retry_stale_direct_vm_auth(response) |
379 | 396 | return super()._should_retry(response) |
380 | 397 |
|
381 | 398 | @override |
@@ -594,6 +611,8 @@ def __init__( |
594 | 611 | ) |
595 | 612 | self.browser_route_cache = _browser_route_cache or BrowserRouteCache() |
596 | 613 | self._browser_routing = browser_routing_config_from_env() |
| 614 | + install_async_direct_vm_auth_stripping(self._client) |
| 615 | + install_async_stale_direct_vm_auth_eviction(self._client) |
597 | 616 |
|
598 | 617 | @cached_property |
599 | 618 | def deployments(self) -> AsyncDeploymentsResource: |
@@ -758,13 +777,21 @@ async def _prepare_options(self, options: Any) -> Any: |
758 | 777 |
|
759 | 778 | @override |
760 | 779 | async def _prepare_request(self, request: httpx.Request) -> None: |
761 | | - strip_direct_vm_auth(request, cache=self.browser_route_cache) |
| 780 | + prepare_direct_vm_request(request, cache=self.browser_route_cache) |
| 781 | + |
| 782 | + @override |
| 783 | + def _should_retry_on_connection_error(self, request: httpx.Request) -> bool: |
| 784 | + return should_retry_direct_vm_connection_error(request) |
762 | 785 |
|
763 | 786 | @override |
764 | 787 | def _should_retry(self, response: httpx.Response) -> bool: |
765 | | - if should_retry_stale_direct_vm_auth(response): |
766 | | - maybe_evict_browser_route_from_response(response, cache=self.browser_route_cache) |
767 | | - return True |
| 788 | + if direct_vm_request_body_is_known_unreplayable(response.request): |
| 789 | + return False |
| 790 | + if is_stale_direct_vm_auth_response(response): |
| 791 | + # The route was already evicted by the response hook; retry only when |
| 792 | + # the body can be rebuilt, otherwise the caller sees the original auth |
| 793 | + # failure and a later call goes to the control plane. |
| 794 | + return should_retry_stale_direct_vm_auth(response) |
768 | 795 | return super()._should_retry(response) |
769 | 796 |
|
770 | 797 | @override |
|
0 commit comments