Sync vs Async Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The faceit-python library provides a dual-interface architecture that allows you to interact with the FACEIT API using either synchronous or asynchronous programming patterns. This architecture is built upon the httpx library and ensures full type safety across both execution modes.
The library mirrors its resource structure for both modes, providing SyncDataResource for blocking calls and AsyncDataResource for non-blocking awaitable calls. This choice allows developers to integrate the library into traditional scripts or high-performance asynchronous applications without changing the idiomatic usage of the API.
Sources: README.md:12-18, src/faceit/api/data/init.py:76-118
Core Client Architecture
The architecture relies on a base client hierarchy that abstracts the underlying HTTP transport. The BaseAPIClient defines common logic such as API key validation, header preparation, and retry logic using tenacity.
HTTP Client Hierarchy
The diagram shows the inheritance structure of HTTP clients, separating synchronous and asynchronous implementations.
Sources: src/faceit/http/client.py:61-68, src/faceit/http/client.py:175, src/faceit/http/client.py:204
Synchronous vs Asynchronous Implementation Details
| Feature | Synchronous (SyncClient) | Asynchronous (AsyncClient) |
|---|---|---|
| Underlying Library | httpx.Client | httpx.AsyncClient |
| Concurrency Control | Thread-blocking | asyncio.Semaphore based rate limiting |
| Retry Mechanism | tenacity.Retrying | tenacity.AsyncRetrying |
| Context Management | __enter__ / __exit__ | __aenter__ / __aexit__ |
| Connection Closing | .close() | await .aclose() |
Sources: src/faceit/http/client.py:177-202, src/faceit/http/client.py:204-266
Resource Aggregation
Resources like Players, Teams, and Games are aggregated into high-level entry points. The SyncDataResource and AsyncDataResource classes act as containers for these specific resource modules.
Aggregator Logic
The @resource_aggregator decorator automatically initializes the specific resource classes (e.g., SyncPlayers or AsyncPlayers) and attaches them to the resource object. It also handles the distinction between "Model" mode (parsed Pydantic models) and "Raw" mode (raw dictionaries).
The flow shows how resources are bound to their respective clients and modes.
Sources: src/faceit/api/aggregator.py:73-86, src/faceit/api/data/init.py:76-118
Paginated Data Flow
Pagination is handled through specialized iterators that mirror the library's sync/async split. These iterators manage API offset and limit parameters automatically.
Sync vs Async Iteration Pattern
For fetching multiple pages of data (e.g., player history), the library uses SyncPageIterator and AsyncPageIterator.
- Synchronous: Uses a standard
Iteratorwhere__next__blocks until the next page is fetched. - Asynchronous: Uses an
AsyncIteratorwhere__anext__is anawaitablecall.
Sequence of events during asynchronous paginated data retrieval.
Sources: src/faceit/api/pagination.py:382-410, src/faceit/api/pagination.py:489-518
Comparison of Collection Methods
| Method | Sync Usage | Async Usage |
|---|---|---|
| Single Item | resource.get(id) | await resource.get(id) |
| Manual Paging | resource.items(limit=20) | await resource.items(limit=20) |
| Auto Collection | resource.all_items() | await resource.all_items() |
| Iterator | for page in SyncPageIterator(...) | async for page in AsyncPageIterator(...) |
Sources: src/faceit/api/data/players.py:165, src/faceit/api/data/players.py:445, src/faceit/api/pagination.py:412, src/faceit/api/pagination.py:510
Adaptive Async Rate Limiting
The asynchronous architecture includes an adaptive limit system to handle connection stability, specifically for SSL errors which may occur during high concurrency.
- Registration: If an SSL error is detected,
_register_ssl_erroris called. - Reduction: The concurrent connection limit is halved if the error threshold is met.
- Recovery:
_check_connection_recoveryperiodically increases the limit back toward the initial maximum if no errors occur for a specific interval.
Sources: src/faceit/http/client.py:270-320, src/faceit/http/client.py:322-351
Conclusion
The faceit-python architecture ensures that developers have a consistent experience regardless of their choice between synchronous or asynchronous execution. By abstracting transport logic into SyncClient and AsyncClient and mirroring resource modules, the library maintains strict type safety and idiomatic Python patterns while providing advanced features like adaptive rate limiting and automated pagination.