proxywhirl.rotator.async¶
Async proxy rotation implementation using httpx.AsyncClient.
Classes¶
Async proxy rotator with automatic failover and intelligent rotation. |
|
LRU cache for httpx.AsyncClient instances with automatic eviction. |
Module Contents¶
- class proxywhirl.rotator.async_.AsyncProxyWhirl(proxies=None, strategy=None, config=None, retry_policy=None, bootstrap=None)[source]¶
Bases:
proxywhirl.rotator.base.ProxyRotatorBaseAsync proxy rotator with automatic failover and intelligent rotation.
Provides async HTTP methods (GET, POST, PUT, DELETE, PATCH) that automatically rotate through a pool of proxies, with intelligent failover on connection errors.
Example:
from proxywhirl import AsyncProxyWhirl, Proxy async with AsyncProxyWhirl() as rotator: await rotator.add_proxy("http://proxy1.example.com:8080") await rotator.add_proxy("http://proxy2.example.com:8080") response = await rotator.get("https://httpbin.org/ip") print(response.json())
Initialize AsyncProxyWhirl.
- Parameters:
proxies (list[proxywhirl.models.Proxy] | None) – Initial list of proxies (optional)
strategy (proxywhirl.strategies.RotationStrategy | str | None) – Rotation strategy instance or name string. Supported names:
round-robin,random,weighted,least-used,performance-based,session,geo-targeted. Defaults to RoundRobinStrategy.config (proxywhirl.models.ProxyConfiguration | None) – Configuration settings (default: ProxyConfiguration())
retry_policy (proxywhirl.retry.RetryPolicy | None) – Retry policy configuration (default: RetryPolicy())
bootstrap (proxywhirl.models.BootstrapConfig | bool | None) – Bootstrap configuration for lazy proxy fetching. False disables auto-bootstrap (for manual proxy management). True or None uses default BootstrapConfig.
- async add_proxy(proxy)[source]¶
Add a proxy to the pool.
- Parameters:
proxy (proxywhirl.models.Proxy | str) – Proxy instance or URL string
- Return type:
None
- async clear_unhealthy_proxies()[source]¶
Remove all unhealthy and dead proxies from the pool.
- Returns:
Number of proxies removed
- Return type:
- async delete(url, **kwargs)[source]¶
Make async DELETE request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- async get(url, **kwargs)[source]¶
Make async GET request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- get_circuit_breaker_states()[source]¶
Get circuit breaker states for all proxies.
- Returns:
Mapping of proxy IDs to their circuit breaker instances.
- Return type:
- async get_proxy()[source]¶
Get the next proxy from the pool using the rotation strategy.
- Returns:
Next proxy to use
- Raises:
ProxyPoolEmptyError – If no healthy proxies available
- Return type:
proxywhirl.models.Proxy
- get_retry_metrics()[source]¶
Get retry metrics.
- Returns:
RetryMetrics instance with current metrics
- Return type:
proxywhirl.retry.RetryMetrics
- async head(url, **kwargs)[source]¶
Make async HEAD request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- async options(url, **kwargs)[source]¶
Make async OPTIONS request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- async patch(url, **kwargs)[source]¶
Make async PATCH request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- async post(url, **kwargs)[source]¶
Make async POST request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- async put(url, **kwargs)[source]¶
Make async PUT request.
- Parameters:
url (str)
kwargs (Any)
- Return type:
httpx.Response
- async remove_proxy(proxy_id)[source]¶
Remove a proxy from the pool.
- Parameters:
proxy_id (str) – UUID of proxy to remove
- Return type:
None
- set_strategy(strategy, *, atomic=True)[source]¶
Hot-swap the rotation strategy without restarting.
This method implements atomic strategy swapping to ensure: - New requests immediately use the new strategy - In-flight requests complete with their original strategy - No requests are dropped during the swap - Swap completes in <100ms (SC-009)
- Parameters:
strategy (proxywhirl.strategies.RotationStrategy | str) – New strategy instance or name string. Supported names:
round-robin,random,weighted,least-used,performance-based,session,geo-targeted.atomic (bool) – If True (default), ensures atomic swap. If False, allows immediate replacement (faster but may affect in-flight requests)
- Return type:
None
Example
>>> async with AsyncProxyWhirl(strategy="round-robin") as rotator: ... # ... after some requests ... ... rotator.set_strategy("performance-based") # Hot-swap ... # New requests now use performance-based strategy
- Thread Safety:
Thread-safe via atomic reference swap. Multiple threads can call this method safely without race conditions.
- Performance:
Target: <100ms for hot-swap completion (SC-009) Typical: <10ms for strategy instance creation and assignment
- class proxywhirl.rotator.async_.LRUAsyncClientPool(maxsize=100)[source]¶
LRU cache for httpx.AsyncClient instances with automatic eviction.
When the pool reaches maxsize, the least recently used client is closed and removed to prevent unbounded memory growth.
Supports dictionary-like access for backward compatibility with tests.
Initialize LRU async client pool.
- Parameters:
maxsize (int) – Maximum number of clients to cache (default: 100)
- async get(proxy_id)[source]¶
Get a client from the pool, marking it as recently used.
- Parameters:
proxy_id (str) – Proxy ID to look up
- Returns:
Client if found, None otherwise
- Return type:
httpx.AsyncClient | None