|
5 | 5 | import inspect |
6 | 6 | from asyncio import Lock |
7 | 7 | from datetime import datetime, timedelta, timezone |
| 8 | +from functools import lru_cache |
8 | 9 | from typing import TYPE_CHECKING, Any, cast |
9 | 10 |
|
10 | 11 | from browserforge.injectors.playwright import AsyncNewContext |
|
29 | 30 |
|
30 | 31 | logger = getLogger(__name__) |
31 | 32 |
|
32 | | -# Cache Playwright signatures to avoid overhead in critical path |
33 | | -_launch_persistent_context_params = set(inspect.signature(PlaywrightBrowserType.launch_persistent_context).parameters) |
34 | | -_new_context_params = set(inspect.signature(Browser.new_context).parameters) |
35 | 33 |
|
36 | | -_common_context_options = _launch_persistent_context_params & _new_context_params |
37 | | -_persistent_unique_context_options = _launch_persistent_context_params - _new_context_params |
38 | | -_incognito_unique_context_options = _new_context_params - _launch_persistent_context_params |
| 34 | +# Cache Playwright signatures to avoid overhead in critical path |
| 35 | +@lru_cache(maxsize=1) |
| 36 | +def _get_context_params_cache() -> dict[str, set[str]]: |
| 37 | + launch_persistent_params = set(inspect.signature(PlaywrightBrowserType.launch_persistent_context).parameters) |
| 38 | + new_context_params = set(inspect.signature(Browser.new_context).parameters) |
| 39 | + return { |
| 40 | + 'common': launch_persistent_params & new_context_params, |
| 41 | + 'persistent_unique': launch_persistent_params - new_context_params, |
| 42 | + 'incognito_unique': new_context_params - launch_persistent_params, |
| 43 | + } |
39 | 44 |
|
40 | 45 |
|
41 | 46 | @docs_group('Browser management') |
@@ -233,23 +238,25 @@ async def _create_browser_context( |
233 | 238 | """ |
234 | 239 | browser_new_context_options = dict(browser_new_context_options) if browser_new_context_options else {} |
235 | 240 |
|
| 241 | + params_cache = _get_context_params_cache() |
| 242 | + |
236 | 243 | filtered_options = {} |
237 | 244 | for key, value in browser_new_context_options.items(): |
238 | 245 | if self._use_incognito_pages: |
239 | 246 | # Incognito mode (new_context) |
240 | | - if key in _common_context_options or key in _incognito_unique_context_options: |
| 247 | + if key in params_cache['common'] or key in params_cache['incognito_unique']: |
241 | 248 | filtered_options[key] = value |
242 | | - elif key in _persistent_unique_context_options: |
| 249 | + elif key in params_cache['persistent_unique']: |
243 | 250 | logger.warning( |
244 | 251 | f'Option "{key}" is only supported in persistent context mode ' |
245 | 252 | '(use_incognito_pages=False) and will be ignored.' |
246 | 253 | ) |
247 | 254 | else: |
248 | 255 | raise TypeError(f'"{key}" is not a valid Playwright context option.') |
249 | | - elif key in _common_context_options or key in _persistent_unique_context_options: |
| 256 | + elif key in params_cache['common'] or key in params_cache['persistent_unique']: |
250 | 257 | # Persistent mode (launch_persistent_context) |
251 | 258 | filtered_options[key] = value |
252 | | - elif key in _incognito_unique_context_options: |
| 259 | + elif key in params_cache['incognito_unique']: |
253 | 260 | logger.warning( |
254 | 261 | f'Option "{key}" is only supported in incognito context mode ' |
255 | 262 | '(use_incognito_pages=True) and will be ignored.' |
|
0 commit comments