Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions protosocket-rpc/src/client/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ impl<Connector: ClientConnector> ConnectionPool<Connector> {
}
}

/// Get a consistent connection from the pool for a given key.
pub async fn get_connection_for_key(
&self,
key: usize,
) -> crate::Result<RpcClient<Connector::Request, Connector::Response>> {
let slot = key % self.connections.len();

self.get_connection_by_slot(slot).await
}

/// Get a connection from the pool.
pub async fn get_connection(
&self,
Expand All @@ -72,6 +82,14 @@ impl<Connector: ClientConnector> ConnectionPool<Connector> {
// Safety: This is executed on a thread, in only one place. It cannot be borrowed anywhere else.
let slot = THREAD_LOCAL_SMALL_RANDOM
.with_borrow_mut(|rng| rng.random_range(0..self.connections.len()));

self.get_connection_by_slot(slot).await
}

async fn get_connection_by_slot(
&self,
slot: usize,
) -> crate::Result<RpcClient<Connector::Request, Connector::Response>> {
let connection_state = &self.connections[slot];

// The connection state requires a mutex, so I need to keep await out of the scope to satisfy clippy (and for paranoia).
Expand Down
Loading