Skip to content

Commit 6bfd87e

Browse files
authored
Rpc-client function for a consistent connection from the pool (#13)
We need to bypass the random connection selector in order to ensure that a specific key always uses a specific connection.
1 parent 4a3c692 commit 6bfd87e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

protosocket-rpc/src/client/connection_pool.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ impl<Connector: ClientConnector> ConnectionPool<Connector> {
6161
}
6262
}
6363

64+
/// Get a consistent connection from the pool for a given key.
65+
pub async fn get_connection_for_key(
66+
&self,
67+
key: usize,
68+
) -> crate::Result<RpcClient<Connector::Request, Connector::Response>> {
69+
let slot = key % self.connections.len();
70+
71+
self.get_connection_by_slot(slot).await
72+
}
73+
6474
/// Get a connection from the pool.
6575
pub async fn get_connection(
6676
&self,
@@ -72,6 +82,14 @@ impl<Connector: ClientConnector> ConnectionPool<Connector> {
7282
// Safety: This is executed on a thread, in only one place. It cannot be borrowed anywhere else.
7383
let slot = THREAD_LOCAL_SMALL_RANDOM
7484
.with_borrow_mut(|rng| rng.random_range(0..self.connections.len()));
85+
86+
self.get_connection_by_slot(slot).await
87+
}
88+
89+
async fn get_connection_by_slot(
90+
&self,
91+
slot: usize,
92+
) -> crate::Result<RpcClient<Connector::Request, Connector::Response>> {
7593
let connection_state = &self.connections[slot];
7694

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

0 commit comments

Comments
 (0)