Skip to content

Commit

Permalink
catalog connection error handling: avoid a copy
Browse files Browse the repository at this point in the history
also avoid allocating string for port when constructing pg connection string
  • Loading branch information
serprex committed Dec 15, 2023
1 parent 5ef7435 commit 9e0aba7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions nexus/postgres-connection/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Write;
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
use postgres_openssl::MakeTlsConnector;
use pt::peerdb_peers::PostgresConfig;
Expand All @@ -10,15 +11,9 @@ pub fn get_pg_connection_string(config: &PostgresConfig) -> String {
connection_string.push(':');
connection_string.push_str(&urlencoding::encode(&config.password));
}
connection_string.push('@');
connection_string.push_str(&config.host);
connection_string.push(':');
connection_string.push_str(&config.port.to_string());
connection_string.push('/');
connection_string.push_str(&config.database);

// Add the timeout as a query parameter, sslmode changes here appear to be useless
connection_string.push_str("?connect_timeout=15");
write!(connection_string, "@{}:{}/{}?connect_timeout=15", config.host, config.port, config.database).ok();

connection_string
}
Expand Down
4 changes: 2 additions & 2 deletions nexus/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{HashMap, HashSet},
fmt::Write,
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -1376,8 +1377,7 @@ pub async fn main() -> anyhow::Result<()> {
buf.put_i32(0);
buf.put(&b"FATAL"[..]);
buf.put_u8(0);
let error_message = format!("Failed to connect to catalog: {}", e);
buf.put(error_message.as_bytes());
write!(buf, "Failed to connect to catalog: {e}").ok();
buf.put_u8(0);
buf.put_u8(b'\0');

Expand Down

0 comments on commit 9e0aba7

Please sign in to comment.