diff --git a/fetch.py b/fetch.py index de5ba34..3eb5789 100644 --- a/fetch.py +++ b/fetch.py @@ -9,6 +9,10 @@ from misc import _log config = None +# Cache for is_usable_proxy() - avoids repeated validation of same proxy strings +_proxy_valid_cache = {} +_proxy_valid_cache_max = 10000 + def tor_proxy_url(torhost): """Generate Tor SOCKS5 proxy URL with random credentials for circuit isolation.""" @@ -616,7 +620,24 @@ def is_usable_proxy(proxy): - Malformed strings - Invalid port (0, >65535) - Private/reserved ranges + + Results are cached to avoid repeated validation of the same proxy strings. """ + # Check cache first + if proxy in _proxy_valid_cache: + return _proxy_valid_cache[proxy] + + result = _validate_proxy(proxy) + + # Cache result (with size limit) + if len(_proxy_valid_cache) < _proxy_valid_cache_max: + _proxy_valid_cache[proxy] = result + + return result + + +def _validate_proxy(proxy): + """Internal validation logic for is_usable_proxy.""" try: if ':' not in proxy: return False