pub struct ShardClientPool { /* private fields */ }Expand description
A pool of shard client connections.
The connection pool maintains a cached set of connections to shards, creating new connections on demand and reusing existing ones.
§Thread Safety
The pool is thread-safe and can be shared across multiple tasks.
§Example
use phago_distributed::rpc::client::ShardClientPool;
let pool = ShardClientPool::new();
pool.register_shard(ShardId::new(0), "127.0.0.1:8080".parse().unwrap());
let client = pool.get_client(ShardId::new(0)).await?;
let health = client.health_check(tarpc::context::current()).await?;Implementations§
Source§impl ShardClientPool
impl ShardClientPool
Sourcepub fn with_config(config: ClientConfig) -> Self
pub fn with_config(config: ClientConfig) -> Self
Create a new connection pool with custom configuration.
Sourcepub async fn register_shard(&self, shard_id: ShardId, addr: SocketAddr)
pub async fn register_shard(&self, shard_id: ShardId, addr: SocketAddr)
Register a shard’s address.
This does not establish a connection immediately; connections
are created lazily when get_client is called.
Sourcepub async fn unregister_shard(&self, shard_id: ShardId)
pub async fn unregister_shard(&self, shard_id: ShardId)
Unregister a shard and close any cached connection.
Sourcepub async fn get_client(
&self,
shard_id: ShardId,
) -> Result<ShardServiceClient, Error>
pub async fn get_client( &self, shard_id: ShardId, ) -> Result<ShardServiceClient, Error>
Get a client for the specified shard.
Returns a cached client if available, otherwise creates a new connection.
§Errors
Returns an error if the shard is not registered or if the connection fails.
Sourcepub async fn get_all_clients(&self) -> Vec<(ShardId, ShardServiceClient)>
pub async fn get_all_clients(&self) -> Vec<(ShardId, ShardServiceClient)>
Get clients for all registered shards.
Attempts to connect to all shards, returning successfully connected clients. Failed connections are logged but do not cause the entire operation to fail.
Sourcepub async fn shard_count(&self) -> usize
pub async fn shard_count(&self) -> usize
Get the number of registered shards.
Sourcepub async fn cached_connection_count(&self) -> usize
pub async fn cached_connection_count(&self) -> usize
Get the number of cached connections.
Sourcepub async fn clear_cache(&self)
pub async fn clear_cache(&self)
Clear all cached connections.
This forces new connections to be created on the next get_client call.
Sourcepub async fn invalidate_client(&self, shard_id: ShardId)
pub async fn invalidate_client(&self, shard_id: ShardId)
Remove a cached connection for a specific shard.
Useful for forcing a reconnection after a failure.