PubSubClientFactory class¶
fastpubsub.clients.factory.PubSubClientFactory
¶
Async-safe factory for Pub/Sub clients with singleton caching.
This factory ensures that only one client instance is created per unique combination of project_id and configuration. This follows Google's recommendation to reuse Pub/Sub clients for better performance.
The caching strategy
- PublisherClient: Cached by (project_id, enable_ordering)
- SubscriberClient: Cached by project_id
- Uses asyncio.Lock for async-safe double-checked locking
- Safe for concurrent access from multiple coroutines
get_publisher
async
classmethod
¶
Get or create a cached PublisherClient.
| PARAMETER | DESCRIPTION |
|---|---|
project_id
|
The Google Cloud project ID.
TYPE:
|
enable_ordering
|
Whether to enable message ordering.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PublisherClient
|
A cached or newly created PublisherClient instance. |
Source code in fastpubsub/clients/factory.py
get_subscriber
async
classmethod
¶
Get or create a cached SubscriberClient.
| PARAMETER | DESCRIPTION |
|---|---|
project_id
|
The Google Cloud project ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SubscriberClient
|
A cached or newly created SubscriberClient instance. |
Source code in fastpubsub/clients/factory.py
close_all
async
classmethod
¶
Close all cached clients.
This method should be called during application shutdown to ensure all gRPC connections are properly closed.
Source code in fastpubsub/clients/factory.py
clear_cache
classmethod
¶
Clear the client cache without closing connections.
This is primarily useful for testing purposes.