Skip to content

Commit 20ceb72

Browse files
authored
print session duration; connect to all servers when validating (#11)
1 parent eb45d65 commit 20ceb72

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

src/main.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ async fn main() {
123123

124124
// Client goes to another thread, bye.
125125
tokio::task::spawn(async move {
126-
println!(
127-
">> Client {:?} connected, transaction pooling: {}",
128-
addr, transaction_mode
129-
);
126+
let start = chrono::offset::Utc::now().naive_utc();
127+
128+
println!(">> Client {:?} connected", addr);
130129

131130
match client::Client::startup(
132131
socket,
@@ -142,7 +141,13 @@ async fn main() {
142141

143142
match client.handle(pool).await {
144143
Ok(()) => {
145-
println!(">> Client {:?} disconnected.", addr);
144+
let duration = chrono::offset::Utc::now().naive_utc() - start;
145+
146+
println!(
147+
">> Client {:?} disconnected, session duration: {}",
148+
addr,
149+
format_duration(&duration)
150+
);
146151
}
147152

148153
Err(err) => {
@@ -159,3 +164,41 @@ async fn main() {
159164
});
160165
}
161166
}
167+
168+
/// Format chrono::Duration to be more human-friendly.
169+
///
170+
/// # Arguments
171+
///
172+
/// * `duration` - A duration of time
173+
fn format_duration(duration: &chrono::Duration) -> String {
174+
let seconds = {
175+
let seconds = duration.num_seconds() % 60;
176+
if seconds < 10 {
177+
format!("0{}", seconds)
178+
} else {
179+
format!("{}", seconds)
180+
}
181+
};
182+
183+
let minutes = {
184+
let minutes = duration.num_minutes() % 60;
185+
if minutes < 10 {
186+
format!("0{}", minutes)
187+
} else {
188+
format!("{}", minutes)
189+
}
190+
};
191+
192+
let hours = {
193+
let hours = duration.num_hours() % 24;
194+
if hours < 10 {
195+
format!("0{}", hours)
196+
} else {
197+
format!("{}", hours)
198+
}
199+
};
200+
201+
let days = duration.num_days().to_string();
202+
203+
format!("{}d {}:{}:{}", days, hours, minutes, seconds)
204+
}

src/pool.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,27 @@ impl ConnectionPool {
109109
/// Connect to all shards and grab server information.
110110
/// Return server information we will pass to the clients
111111
/// when they connect.
112+
/// This also warms up the pool for clients that connect when
113+
/// the pooler starts up.
112114
pub async fn validate(&mut self) -> Result<BytesMut, Error> {
113115
let mut server_infos = Vec::new();
114116

115117
for shard in 0..self.shards() {
116-
// TODO: query all primary and replicas in the shard configuration.
117-
let connection = match self.get(Some(shard), None).await {
118-
Ok(conn) => conn,
119-
Err(err) => {
120-
println!("> Shard {} down or misconfigured.", shard);
121-
return Err(err);
122-
}
123-
};
118+
for _ in 0..self.replicas(shard) {
119+
let connection = match self.get(Some(shard), None).await {
120+
Ok(conn) => conn,
121+
Err(err) => {
122+
println!("> Shard {} down or misconfigured.", shard);
123+
return Err(err);
124+
}
125+
};
124126

125-
let mut proxy = connection.0;
126-
let _address = connection.1;
127-
let server = &mut *proxy;
127+
let mut proxy = connection.0;
128+
let _address = connection.1;
129+
let server = &mut *proxy;
128130

129-
server_infos.push(server.server_info());
131+
server_infos.push(server.server_info());
132+
}
130133
}
131134

132135
// TODO: compare server information to make sure
@@ -326,6 +329,10 @@ impl ConnectionPool {
326329
pub fn shards(&self) -> usize {
327330
self.databases.len()
328331
}
332+
333+
pub fn replicas(&self, shard: usize) -> usize {
334+
self.addresses[shard].len()
335+
}
329336
}
330337

331338
pub struct ServerPool {

0 commit comments

Comments
 (0)