Skip to content

Commit 069d760

Browse files
authored
Fix incorrect routing for replicas (#139)
* Fix incorrect routing for replicas * name
1 parent 902fafd commit 069d760

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/config.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ pub struct Address {
6363
pub shard: usize,
6464
pub database: String,
6565
pub role: Role,
66-
pub instance_index: usize,
66+
pub replica_number: usize,
67+
pub address_index: usize,
6768
pub username: String,
68-
pub poolname: String,
69+
pub pool_name: String,
6970
}
7071

7172
impl Default for Address {
@@ -75,11 +76,12 @@ impl Default for Address {
7576
host: String::from("127.0.0.1"),
7677
port: String::from("5432"),
7778
shard: 0,
78-
instance_index: 0,
79+
address_index: 0,
80+
replica_number: 0,
7981
database: String::from("database"),
8082
role: Role::Replica,
8183
username: String::from("username"),
82-
poolname: String::from("poolname"),
84+
pool_name: String::from("pool_name"),
8385
}
8486
}
8587
}
@@ -88,11 +90,11 @@ impl Address {
8890
/// Address name (aka database) used in `SHOW STATS`, `SHOW DATABASES`, and `SHOW POOLS`.
8991
pub fn name(&self) -> String {
9092
match self.role {
91-
Role::Primary => format!("{}_shard_{}_primary", self.poolname, self.shard),
93+
Role::Primary => format!("{}_shard_{}_primary", self.pool_name, self.shard),
9294

9395
Role::Replica => format!(
9496
"{}_shard_{}_replica_{}",
95-
self.poolname, self.shard, self.instance_index
97+
self.pool_name, self.shard, self.replica_number
9698
),
9799
}
98100
}

src/pool.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl ConnectionPool {
102102
let shard = &pool_config.shards[&shard_idx];
103103
let mut pools = Vec::new();
104104
let mut servers = Vec::new();
105+
let mut address_index = 0;
105106
let mut replica_number = 0;
106107

107108
for server in shard.servers.iter() {
@@ -120,13 +121,15 @@ impl ConnectionPool {
120121
host: server.0.clone(),
121122
port: server.1.to_string(),
122123
role: role,
123-
instance_index: replica_number,
124+
address_index,
125+
replica_number,
124126
shard: shard_idx.parse::<usize>().unwrap(),
125127
username: user_info.username.clone(),
126-
poolname: pool_name.clone(),
128+
pool_name: pool_name.clone(),
127129
};
128130

129131
address_id += 1;
132+
address_index += 1;
130133

131134
if role == Role::Replica {
132135
replica_number += 1;
@@ -276,7 +279,7 @@ impl ConnectionPool {
276279
self.stats.client_waiting(process_id, address.id);
277280

278281
// Check if we can connect
279-
let mut conn = match self.databases[address.shard][address.instance_index]
282+
let mut conn = match self.databases[address.shard][address.address_index]
280283
.get()
281284
.await
282285
{

0 commit comments

Comments
 (0)