pub struct BackendSelector { /* private fields */ }Expand description
Selects backend servers using round-robin with load tracking
§Thread Safety
This struct is designed for concurrent access across multiple threads. The round-robin counter and pending counts use atomic operations for lock-free performance.
§Load Balancing
- Strategy: Round-robin rotation through available backends
- Tracking: Atomic counters track pending commands per backend
- Monitoring: Load statistics available via
backend_load()
§Examples
let mut selector = BackendSelector::new();
selector.add_backend(
BackendId::from_index(0),
"backend-1".to_string(),
provider,
);
// Route commands
let backend = selector.route_command_sync(ClientId::new(), "LIST")?;Implementations§
Source§impl BackendSelector
impl BackendSelector
Sourcepub fn add_backend(
&mut self,
backend_id: BackendId,
name: String,
provider: DeadpoolConnectionProvider,
)
pub fn add_backend( &mut self, backend_id: BackendId, name: String, provider: DeadpoolConnectionProvider, )
Add a backend server to the router
Sourcepub fn route_command_sync(
&self,
_client_id: ClientId,
_command: &str,
) -> Result<BackendId>
pub fn route_command_sync( &self, _client_id: ClientId, _command: &str, ) -> Result<BackendId>
Select a backend for the given command using round-robin Returns the backend ID to use for this command
Sourcepub fn complete_command_sync(&self, backend_id: BackendId)
pub fn complete_command_sync(&self, backend_id: BackendId)
Mark a command as complete, decrementing the pending count
Sourcepub fn get_backend_provider(
&self,
backend_id: BackendId,
) -> Option<&DeadpoolConnectionProvider>
pub fn get_backend_provider( &self, backend_id: BackendId, ) -> Option<&DeadpoolConnectionProvider>
Get the connection provider for a backend
Sourcepub fn backend_count(&self) -> usize
pub fn backend_count(&self) -> usize
Get the number of backends
Sourcepub fn backend_load(&self, backend_id: BackendId) -> Option<usize>
pub fn backend_load(&self, backend_id: BackendId) -> Option<usize>
Get backend load (pending requests) for monitoring
Sourcepub fn try_acquire_stateful(&self, backend_id: BackendId) -> bool
pub fn try_acquire_stateful(&self, backend_id: BackendId) -> bool
Try to acquire a stateful connection slot for hybrid mode Returns true if acquisition succeeded (within max_connections-1 limit) Returns false if all stateful slots are taken (need to keep 1 for PCR)
Sourcepub fn release_stateful(&self, backend_id: BackendId)
pub fn release_stateful(&self, backend_id: BackendId)
Release a stateful connection slot
Sourcepub fn stateful_count(&self, backend_id: BackendId) -> Option<usize>
pub fn stateful_count(&self, backend_id: BackendId) -> Option<usize>
Get the number of stateful connections for a backend