fix(proxy): add missing make_sticky_selector function

Tests imported make_sticky_selector but it was never implemented.
Hash-based (MD5) consistent selector — same key always returns the
same proxy, distributes across the pool.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-02-27 11:10:20 +01:00
parent c345746fbc
commit ede7983a77

View File

@@ -57,6 +57,27 @@ def make_round_robin_cycler(proxy_urls: list[str]):
return next_proxy return next_proxy
def make_sticky_selector(proxy_urls: list[str]):
"""Hash-based sticky proxy selector.
Returns a callable: select_proxy(key: str) -> str | None
The same key always maps to the same proxy (consistent hashing).
Returns None-returning callable if no proxies configured.
"""
if not proxy_urls:
return lambda key: None
n = len(proxy_urls)
def select_proxy(key: str) -> str:
import hashlib
idx = int(hashlib.md5(key.encode(), usedforsecurity=False).hexdigest(), 16) % n
return proxy_urls[idx]
return select_proxy
def make_tiered_cycler( def make_tiered_cycler(
primary_urls: list[str], primary_urls: list[str],
fallback_urls: list[str], fallback_urls: list[str],