ProxyManager Reference
ProxyManager¶
ProxyManager orchestrates leasing, releasing, and maintenance of proxies while
delegating persistence to an IStorage adapter.
from pharox import ProxyManager
manager = ProxyManager(storage)
Constructor¶
ProxyManager(storage: IStorage)
storage: Concrete implementation ofpharox.storage.IStorage.- The manager keeps no in-memory state besides registered callbacks.
Core Methods¶
acquire_proxy¶
def acquire_proxy(
pool_name: str,
consumer_name: str | None = None,
duration_seconds: int = 300,
filters: ProxyFilters | None = None,
selector: SelectorStrategy | None = None,
) -> Lease | None
- Validates
duration_seconds > 0. - Auto-registers the default consumer (
"default") usingstorage.ensure_consumer. - Calls
storage.cleanup_expired_leases()before searching for a proxy. - Uses
storage.find_available_proxy+storage.create_lease. - Returns
Nonewhen no eligible proxy exists. - Accepts an optional
selectorhint (SelectorStrategy) to influence the order in which adapters return proxies. Defaults to first-available if omitted.
release_proxy¶
def release_proxy(lease: Lease) -> None
- Delegates to
storage.release_lease. - Triggers release callbacks after storage completes.
cleanup_expired_leases¶
def cleanup_expired_leases() -> int
- Pass-through to
storage.cleanup_expired_leases. - Returns number of leases released.
with_lease¶
@contextmanager
def with_lease(
pool_name: str,
consumer_name: str | None = None,
duration_seconds: int = 300,
filters: ProxyFilters | None = None,
selector: SelectorStrategy | None = None,
) -> Iterator[Lease | None]
- Wraps
acquire_proxyand guaranteesrelease_proxyin afinallyblock. -
Yields
Nonewhen acquisition fails so callers can retry gracefully. -
Yields
Nonewhen acquisition fails so callers can retry gracefully.
acquire_proxy_with_retry¶
def acquire_proxy_with_retry(
pool_name: str,
consumer_name: str | None = None,
duration_seconds: int = 300,
filters: ProxyFilters | None = None,
selector: SelectorStrategy | None = None,
max_attempts: int = 3,
backoff_seconds: float = 0.5,
backoff_multiplier: float = 2.0,
max_backoff_seconds: float | None = None,
sleep_fn: Callable[[float], None] | None = None,
) -> Lease | None
- Calls
acquire_proxyup tomax_attemptstimes. - Waits
backoff_secondsbetween attempts (grows bybackoff_multiplieruntil capped bymax_backoff_seconds). - Accepts a custom
sleep_fnfor tests; defaults totime.sleep. - Raises
ValueErrorif retry parameters are invalid.
with_retrying_lease¶
@contextmanager
def with_retrying_lease(..., max_attempts: int = 3, backoff_seconds: float = 0.5, ...)
- Wraps
acquire_proxy_with_retryand still guarantees releases on exit. - Use it when a worker should wait for a proxy before giving up.
Selector Strategies¶
SelectorStrategy enumerates the available selection behaviours:
FIRST_AVAILABLE(default): deterministic first-fit ordered bychecked_atdescending, thenproxy.id.LEAST_USED: prioritises proxies with the fewest active leases, tying bychecked_atandproxy.id.ROUND_ROBIN: cycles through the pool in a deterministic order backed by storage so concurrent workers share proxies fairly.
All adapters must support FIRST_AVAILABLE; reference adapters (in-memory and
PostgreSQL) also implement the other strategies. Custom adapters can choose to
ignore optional strategies, but should document the behaviour for callers.
Callback Registration¶
from pharox import (
AcquireEventPayload,
ReleaseEventPayload,
)
manager.register_acquire_callback(Callable[[AcquireEventPayload], None])
manager.register_release_callback(Callable[[ReleaseEventPayload], None])
AcquireEventPayloadincludes the resultingLease(orNone), the pool and consumer names, resolved filters, the selectedSelectorStrategy,started_at/completed_attimestamps, the execution duration in milliseconds, and aPoolStatsSnapshot.ReleaseEventPayloadcontains the releasedLease,released_at, computed lease duration, and the same pool stats snapshot captured after the release.- Callbacks run synchronously; keep them lightweight or hand off to background workers.
PoolStatsSnapshot¶
PoolStatsSnapshot reports aggregated counts per pool:
total_proxies,active_proxies,available_proxiesleased_proxies(proxies with at least one active lease)total_leases(sum ofcurrent_leasesacross the pool)collected_attimestamp for when the snapshot was generated
Async Helpers¶
The core manager is synchronous, but Pharox exposes thin wrappers to make it
ergonomic in asyncio applications:
from pharox import (
acquire_proxy_async,
acquire_proxy_with_retry_async,
release_proxy_async,
with_lease_async,
with_retrying_lease_async,
)
acquire_proxy_asyncandrelease_proxy_asyncdelegate to the synchronous manager viaasyncio.to_thread, keeping event loops responsive.acquire_proxy_with_retry_asyncmirrors the retry helper for async code, exposing the same parameters.with_lease_asyncandwith_retrying_lease_asyncmirror their synchronous counterparts while ensuring leases are released when async blocks exit—even if an exception occurs.
Use these helpers whenever your storage adapter is synchronous but the calling code runs inside an async worker or FastAPI route handler.
Proxy Filters¶
ProxyFiltersaccepts simple equality or geospatial constraints plus optional boolean structure viaall_of,any_of, andnone_of.- Nested filters let you express clauses such as “(country=AR OR country=BR) AND source=latam.”
predicateaccepts a callableCallable[[Proxy], bool]for adapter-level evaluation. Keep predicates side-effect free and fast; SQL adapters fetch extra candidates under the hood when a predicate is present.
Default Consumer Name¶
ProxyManager.DEFAULT_CONSUMER_NAME == "default".- Auto-created on first acquisition without an explicit
consumer_name.
Best Practices¶
- Register callbacks once at process startup to avoid duplicate telemetry.
- Combine
with_leasewithtry/exceptif you need custom error handling. - Schedule
cleanup_expired_leasesperiodically for long-running services. - Use
ProxyFiltersto offload selection logic to storage adapters.