File tree Expand file tree Collapse file tree 5 files changed +28
-17
lines changed
Expand file tree Collapse file tree 5 files changed +28
-17
lines changed Original file line number Diff line number Diff line change @@ -95,3 +95,17 @@ def merge_dict_configs(
9595
9696def is_valid_path (maybe_path : str ) -> bool :
9797 return bool (re .fullmatch (VALID_PATH_PATTERN , maybe_path ))
98+
99+
100+ def optimize_exclude_paths (
101+ exclude_endpoints : typing .Iterable [str ],
102+ ) -> typing .Collection [str ]:
103+ # `in` operator is faster for tuples than for lists
104+ endpoints_to_ignore : typing .Collection [str ] = tuple (exclude_endpoints )
105+
106+ # 10 is just an empirical value, based of measuring the performance
107+ # iterating over a tuple of <10 elements is faster than hashing
108+ if len (endpoints_to_ignore ) >= 10 : # noqa: PLR2004
109+ endpoints_to_ignore = set (endpoints_to_ignore )
110+
111+ return endpoints_to_ignore
Original file line number Diff line number Diff line change 55from fastapi import status
66from starlette .middleware .base import BaseHTTPMiddleware , RequestResponseEndpoint
77
8+ from microbootstrap .helpers import optimize_exclude_paths
89from microbootstrap .instruments .logging_instrument import fill_log_message
9- from .utils import optimize_exclude_paths
1010
1111
1212def build_fastapi_logging_middleware (
Original file line number Diff line number Diff line change 77from litestar .middleware .base import MiddlewareProtocol
88from litestar .status_codes import HTTP_500_INTERNAL_SERVER_ERROR
99
10+ from microbootstrap .helpers import optimize_exclude_paths
1011from microbootstrap .instruments .logging_instrument import fill_log_message
11- from .utils import optimize_exclude_paths
1212
1313
1414def build_litestar_logging_middleware (
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 55import pytest
66
77from microbootstrap import exceptions , helpers
8+ from microbootstrap .helpers import optimize_exclude_paths
89
910
1011@pytest .mark .parametrize (
@@ -146,3 +147,14 @@ def test_merge_dataclasses_configs(
146147 result : DataclassConfig ,
147148) -> None :
148149 assert result == helpers .merge_dataclasses_configs (first_class , second_class )
150+
151+
152+ @pytest .mark .parametrize (
153+ "exclude_paths" ,
154+ [
155+ ["path" ],
156+ ["path" ] * 11 ,
157+ ],
158+ )
159+ def test_optimize_exclude_paths (exclude_paths : list [str ]) -> None :
160+ optimize_exclude_paths (exclude_paths )
You can’t perform that action at this time.
0 commit comments