Skip to content

Commit 44b5e7e

Browse files
authored
use logger lib; minor refactor; sv_* stats (#29)
1 parent 108f571 commit 44b5e7e

File tree

9 files changed

+236
-89
lines changed

9 files changed

+236
-89
lines changed

Cargo.lock

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ statsd = "0.15"
2424
sqlparser = "0.14"
2525
log = "0.4"
2626
arc-swap = "1"
27+
env_logger = "0.9"

src/client.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// We are pretending to the server in this scenario,
33
/// and this module implements that.
44
use bytes::{Buf, BufMut, BytesMut};
5+
use log::error;
56
use tokio::io::{AsyncReadExt, BufReader};
67
use tokio::net::{
78
tcp::{OwnedReadHalf, OwnedWriteHalf},
@@ -253,7 +254,7 @@ impl Client {
253254
let connection = match pool.get(query_router.shard(), query_router.role()).await {
254255
Ok(conn) => conn,
255256
Err(err) => {
256-
println!(">> Could not get connection from pool: {:?}", err);
257+
error!("Could not get connection from pool: {:?}", err);
257258
error_response(&mut self.write, "could not get connection from the pool")
258259
.await?;
259260
continue;
@@ -267,8 +268,9 @@ impl Client {
267268
// Claim this server as mine for query cancellation.
268269
server.claim(self.process_id, self.secret_key);
269270

270-
// Client active
271+
// Client active & server active
271272
self.stats.client_active(self.process_id);
273+
self.stats.server_active(server.process_id());
272274

273275
// Transaction loop. Multiple queries can be issued by the client here.
274276
// The connection belongs to the client until the transaction is over,
@@ -338,7 +340,7 @@ impl Client {
338340
// Release server back to the pool if we are in transaction mode.
339341
// If we are in session mode, we keep the server until the client disconnects.
340342
if self.transaction_mode {
341-
// Report this client as idle.
343+
self.stats.server_idle(server.process_id());
342344
break;
343345
}
344346
}
@@ -420,6 +422,7 @@ impl Client {
420422
self.stats.transaction();
421423

422424
if self.transaction_mode {
425+
self.stats.server_idle(server.process_id());
423426
break;
424427
}
425428
}
@@ -453,6 +456,7 @@ impl Client {
453456
self.stats.transaction();
454457

455458
if self.transaction_mode {
459+
self.stats.server_idle(server.process_id());
456460
break;
457461
}
458462
}
@@ -461,7 +465,7 @@ impl Client {
461465
// Some unexpected message. We either did not implement the protocol correctly
462466
// or this is not a Postgres client we're talking to.
463467
_ => {
464-
println!(">>> Unexpected code: {}", code);
468+
error!("Unexpected code: {}", code);
465469
}
466470
}
467471
}

src/config.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use arc_swap::{ArcSwap, Guard};
2+
use log::{error, info};
23
use once_cell::sync::Lazy;
34
use serde_derive::Deserialize;
45
use tokio::fs::File;
@@ -150,14 +151,14 @@ impl Default for Config {
150151

151152
impl Config {
152153
pub fn show(&self) {
153-
println!("> Pool size: {}", self.general.pool_size);
154-
println!("> Pool mode: {}", self.general.pool_mode);
155-
println!("> Ban time: {}s", self.general.ban_time);
156-
println!(
157-
"> Healthcheck timeout: {}ms",
154+
info!("Pool size: {}", self.general.pool_size);
155+
info!("Pool mode: {}", self.general.pool_mode);
156+
info!("Ban time: {}s", self.general.ban_time);
157+
info!(
158+
"Healthcheck timeout: {}ms",
158159
self.general.healthcheck_timeout
159160
);
160-
println!("> Connection timeout: {}ms", self.general.connect_timeout);
161+
info!("Connection timeout: {}ms", self.general.connect_timeout);
161162
}
162163
}
163164

@@ -171,23 +172,23 @@ pub async fn parse(path: &str) -> Result<(), Error> {
171172
let mut file = match File::open(path).await {
172173
Ok(file) => file,
173174
Err(err) => {
174-
println!("> Config error: {:?}", err);
175+
error!("{:?}", err);
175176
return Err(Error::BadConfig);
176177
}
177178
};
178179

179180
match file.read_to_string(&mut contents).await {
180181
Ok(_) => (),
181182
Err(err) => {
182-
println!("> Config error: {:?}", err);
183+
error!("{:?}", err);
183184
return Err(Error::BadConfig);
184185
}
185186
};
186187

187188
let config: Config = match toml::from_str(&contents) {
188189
Ok(config) => config,
189190
Err(err) => {
190-
println!("> Config error: {:?}", err);
191+
error!("{:?}", err);
191192
return Err(Error::BadConfig);
192193
}
193194
};
@@ -200,7 +201,7 @@ pub async fn parse(path: &str) -> Result<(), Error> {
200201
let mut primary_count = 0;
201202

202203
if shard.1.servers.len() == 0 {
203-
println!("> Shard {} has no servers configured", shard.0);
204+
error!("Shard {} has no servers configured", shard.0);
204205
return Err(Error::BadConfig);
205206
}
206207

@@ -218,8 +219,8 @@ pub async fn parse(path: &str) -> Result<(), Error> {
218219
"primary" => (),
219220
"replica" => (),
220221
_ => {
221-
println!(
222-
"> Shard {} server role must be either 'primary' or 'replica', got: '{}'",
222+
error!(
223+
"Shard {} server role must be either 'primary' or 'replica', got: '{}'",
223224
shard.0, server.2
224225
);
225226
return Err(Error::BadConfig);
@@ -228,12 +229,12 @@ pub async fn parse(path: &str) -> Result<(), Error> {
228229
}
229230

230231
if primary_count > 1 {
231-
println!("> Shard {} has more than on primary configured.", &shard.0);
232+
error!("Shard {} has more than on primary configured", &shard.0);
232233
return Err(Error::BadConfig);
233234
}
234235

235236
if dup_check.len() != shard.1.servers.len() {
236-
println!("> Shard {} contains duplicate server configs.", &shard.0);
237+
error!("Shard {} contains duplicate server configs", &shard.0);
237238
return Err(Error::BadConfig);
238239
}
239240
}
@@ -243,8 +244,8 @@ pub async fn parse(path: &str) -> Result<(), Error> {
243244
"primary" => (),
244245
"replica" => (),
245246
other => {
246-
println!(
247-
"> Query router default_role must be 'primary', 'replica', or 'any', got: '{}'",
247+
error!(
248+
"Query router default_role must be 'primary', 'replica', or 'any', got: '{}'",
248249
other
249250
);
250251
return Err(Error::BadConfig);

0 commit comments

Comments
 (0)