Only set application_name if it's different (#74)

* Only set application_name if it's different

* keep server named pgcat until something else changes
This commit is contained in:
Lev Kokotov
2022-06-05 09:48:06 -07:00
committed by GitHub
parent 37e3a86881
commit bac4e1f52c
2 changed files with 20 additions and 5 deletions

View File

@@ -369,6 +369,7 @@ impl Client {
if server.in_transaction() {
server.query("ROLLBACK").await?;
server.query("DISCARD ALL").await?;
server.set_name("pgcat").await?;
}
return Err(err);
@@ -440,6 +441,7 @@ impl Client {
if server.in_transaction() {
server.query("ROLLBACK").await?;
server.query("DISCARD ALL").await?;
server.set_name("pgcat").await?;
}
self.release();

View File

@@ -54,6 +54,9 @@ pub struct Server {
/// Reports various metrics, e.g. data sent & received.
stats: Reporter,
/// Application name using the server at the moment.
application_name: String,
}
impl Server {
@@ -210,7 +213,7 @@ impl Server {
let (read, write) = stream.into_split();
return Ok(Server {
let mut server = Server {
address: address.clone(),
read: BufReader::new(read),
write: write,
@@ -224,7 +227,12 @@ impl Server {
client_server_map: client_server_map,
connected_at: chrono::offset::Utc::now().naive_utc(),
stats: stats,
});
application_name: String::new(),
};
server.set_name("pgcat").await?;
return Ok(server);
}
// We have an unexpected message from the server during this exchange.
@@ -448,9 +456,14 @@ impl Server {
/// A shorthand for `SET application_name = $1`.
#[allow(dead_code)]
pub async fn set_name(&mut self, name: &str) -> Result<(), Error> {
Ok(self
.query(&format!("SET application_name = '{}'", name))
.await?)
if self.application_name != name {
self.application_name = name.to_string();
Ok(self
.query(&format!("SET application_name = '{}'", name))
.await?)
} else {
Ok(())
}
}
/// Get the servers address.