fix(pass_through_endpoints.py): improve clearing logic - only remove unvisited endpoints (#16400)

simpler than clear all, and try to re-add
This commit is contained in:
Krish Dholakia 2025-11-08 10:33:47 -08:00 committed by Ishaan Jaffer
parent 740be90dae
commit e637733e36

View file

@ -1812,6 +1812,11 @@ class InitPassThroughEndpointHelpers:
"""Clear all pass-through routes from the registry"""
_registered_pass_through_routes.clear()
@staticmethod
def get_registered_pass_through_endpoints_keys() -> List[str]:
"""Get all registered pass-through endpoints from the registry"""
return list(_registered_pass_through_routes.keys())
@staticmethod
def is_registered_pass_through_route(route: str) -> bool:
"""
@ -1914,7 +1919,16 @@ async def initialize_pass_through_endpoints(
combined_pass_through_endpoints = pass_through_endpoints # type: ignore
## clear all existing pass-through endpoints from the FastAPI app routes
InitPassThroughEndpointHelpers.clear_all_pass_through_routes()
# InitPassThroughEndpointHelpers.clear_all_pass_through_routes()
# get a list of all registered pass-through endpoints
# mark the ones that are visited in the list
# remove the ones that are not visited from the list
registered_pass_through_endpoints = (
InitPassThroughEndpointHelpers.get_registered_pass_through_endpoints_keys()
)
visited_endpoints = set()
for endpoint in combined_pass_through_endpoints:
if isinstance(endpoint, PassThroughGenericEndpoint):
@ -1968,6 +1982,8 @@ async def initialize_pass_through_endpoints(
endpoint_id=endpoint_id,
)
visited_endpoints.add(f"{endpoint_id}:exact:{_path}")
# Add wildcard route for sub-paths
if endpoint.get("include_subpath", False) is True:
InitPassThroughEndpointHelpers.add_subpath_route(
@ -1982,10 +1998,17 @@ async def initialize_pass_through_endpoints(
endpoint_id=endpoint_id,
)
visited_endpoints.add(f"{endpoint_id}:subpath:{_path}")
verbose_proxy_logger.debug(
"Added new pass through endpoint: %s (ID: %s)", _path, endpoint_id
)
# remove the ones that are not visited from the list
for endpoint in registered_pass_through_endpoints:
if endpoint not in visited_endpoints:
InitPassThroughEndpointHelpers.remove_endpoint_routes(endpoint)
async def _get_pass_through_endpoints_from_db(
endpoint_id: Optional[str] = None,