mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-23 01:16:30 +00:00
Adds configuration for logging connections and removes get_config from entrypoint (#236)
* Adds configuration for logging connections and removes get_config from entrypoint * typo * rename connection config var and add to toml files * update config log * fmt
This commit is contained in:
@@ -32,6 +32,12 @@ shutdown_timeout = 5000
|
||||
# For how long to ban a server if it fails a health check (seconds).
|
||||
ban_time = 60 # Seconds
|
||||
|
||||
# If we should log client connections
|
||||
log_client_connections = false
|
||||
|
||||
# If we should log client disconnections
|
||||
log_client_disconnections = false
|
||||
|
||||
# Reload config automatically if it changes.
|
||||
autoreload = true
|
||||
|
||||
|
||||
@@ -32,6 +32,12 @@ shutdown_timeout = 60000
|
||||
# For how long to ban a server if it fails a health check (seconds).
|
||||
ban_time = 60 # seconds
|
||||
|
||||
# If we should log client connections
|
||||
log_client_connections = false
|
||||
|
||||
# If we should log client disconnections
|
||||
log_client_disconnections = false
|
||||
|
||||
# Reload config automatically if it changes.
|
||||
autoreload = false
|
||||
|
||||
|
||||
@@ -32,6 +32,12 @@ shutdown_timeout = 60000
|
||||
# For how long to ban a server if it fails a health check (seconds).
|
||||
ban_time = 60 # seconds
|
||||
|
||||
# If we should log client connections
|
||||
log_client_connections = false
|
||||
|
||||
# If we should log client disconnections
|
||||
log_client_disconnections = false
|
||||
|
||||
# Reload config automatically if it changes.
|
||||
autoreload = false
|
||||
|
||||
|
||||
@@ -98,6 +98,8 @@ pub async fn client_entrypoint(
|
||||
shutdown: Receiver<()>,
|
||||
drain: Sender<i32>,
|
||||
admin_only: bool,
|
||||
tls_certificate: Option<String>,
|
||||
log_client_connections: bool,
|
||||
) -> Result<(), Error> {
|
||||
// Figure out if the client wants TLS or not.
|
||||
let addr = stream.peer_addr().unwrap();
|
||||
@@ -105,10 +107,8 @@ pub async fn client_entrypoint(
|
||||
match get_startup::<TcpStream>(&mut stream).await {
|
||||
// Client requested a TLS connection.
|
||||
Ok((ClientConnectionType::Tls, _)) => {
|
||||
let config = get_config();
|
||||
|
||||
// TLS settings are configured, will setup TLS now.
|
||||
if config.general.tls_certificate != None {
|
||||
if tls_certificate != None {
|
||||
debug!("Accepting TLS request");
|
||||
|
||||
let mut yes = BytesMut::new();
|
||||
@@ -118,7 +118,11 @@ pub async fn client_entrypoint(
|
||||
// Negotiate TLS.
|
||||
match startup_tls(stream, client_server_map, shutdown, admin_only).await {
|
||||
Ok(mut client) => {
|
||||
info!("Client {:?} connected (TLS)", addr);
|
||||
if log_client_connections {
|
||||
info!("Client {:?} connected (TLS)", addr);
|
||||
} else {
|
||||
debug!("Client {:?} connected (TLS)", addr);
|
||||
}
|
||||
|
||||
if !client.is_admin() {
|
||||
let _ = drain.send(1).await;
|
||||
@@ -162,7 +166,11 @@ pub async fn client_entrypoint(
|
||||
.await
|
||||
{
|
||||
Ok(mut client) => {
|
||||
info!("Client {:?} connected (plain)", addr);
|
||||
if log_client_connections {
|
||||
info!("Client {:?} connected (plain)", addr);
|
||||
} else {
|
||||
debug!("Client {:?} connected (plain)", addr);
|
||||
}
|
||||
|
||||
if !client.is_admin() {
|
||||
let _ = drain.send(1).await;
|
||||
@@ -203,7 +211,11 @@ pub async fn client_entrypoint(
|
||||
.await
|
||||
{
|
||||
Ok(mut client) => {
|
||||
info!("Client {:?} connected (plain)", addr);
|
||||
if log_client_connections {
|
||||
info!("Client {:?} connected (plain)", addr);
|
||||
} else {
|
||||
debug!("Client {:?} connected (plain)", addr);
|
||||
}
|
||||
|
||||
if !client.is_admin() {
|
||||
let _ = drain.send(1).await;
|
||||
|
||||
@@ -157,6 +157,12 @@ pub struct General {
|
||||
#[serde(default = "General::default_connect_timeout")]
|
||||
pub connect_timeout: u64,
|
||||
|
||||
#[serde(default)] // False
|
||||
pub log_client_connections: bool,
|
||||
|
||||
#[serde(default)] // False
|
||||
pub log_client_disconnections: bool,
|
||||
|
||||
#[serde(default = "General::default_shutdown_timeout")]
|
||||
pub shutdown_timeout: u64,
|
||||
|
||||
@@ -220,6 +226,8 @@ impl Default for General {
|
||||
healthcheck_timeout: Self::default_healthcheck_timeout(),
|
||||
healthcheck_delay: Self::default_healthcheck_delay(),
|
||||
ban_time: Self::default_ban_time(),
|
||||
log_client_connections: false,
|
||||
log_client_disconnections: false,
|
||||
autoreload: false,
|
||||
tls_certificate: None,
|
||||
tls_private_key: None,
|
||||
@@ -517,6 +525,14 @@ impl Config {
|
||||
self.general.healthcheck_timeout
|
||||
);
|
||||
info!("Connection timeout: {}ms", self.general.connect_timeout);
|
||||
info!(
|
||||
"Log client connections: {}",
|
||||
self.general.log_client_connections
|
||||
);
|
||||
info!(
|
||||
"Log client disconnections: {}",
|
||||
self.general.log_client_disconnections
|
||||
);
|
||||
info!("Shutdown timeout: {}ms", self.general.shutdown_timeout);
|
||||
info!("Healthcheck delay: {}ms", self.general.healthcheck_delay);
|
||||
match self.general.tls_certificate.clone() {
|
||||
|
||||
24
src/main.rs
24
src/main.rs
@@ -44,7 +44,7 @@ use jemallocator::Jemalloc;
|
||||
#[global_allocator]
|
||||
static GLOBAL: Jemalloc = Jemalloc;
|
||||
|
||||
use log::{error, info, warn};
|
||||
use log::{debug, error, info, warn};
|
||||
use parking_lot::Mutex;
|
||||
use pgcat::format_duration;
|
||||
use tokio::net::TcpListener;
|
||||
@@ -247,6 +247,8 @@ async fn main() {
|
||||
let drain_tx = drain_tx.clone();
|
||||
let client_server_map = client_server_map.clone();
|
||||
|
||||
let tls_certificate = config.general.tls_certificate.clone();
|
||||
|
||||
tokio::task::spawn(async move {
|
||||
let start = chrono::offset::Utc::now().naive_utc();
|
||||
|
||||
@@ -256,6 +258,8 @@ async fn main() {
|
||||
shutdown_rx,
|
||||
drain_tx,
|
||||
admin_only,
|
||||
tls_certificate.clone(),
|
||||
config.general.log_client_connections,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -263,11 +267,19 @@ async fn main() {
|
||||
|
||||
let duration = chrono::offset::Utc::now().naive_utc() - start;
|
||||
|
||||
info!(
|
||||
"Client {:?} disconnected, session duration: {}",
|
||||
addr,
|
||||
format_duration(&duration)
|
||||
);
|
||||
if config.general.log_client_disconnections {
|
||||
info!(
|
||||
"Client {:?} disconnected, session duration: {}",
|
||||
addr,
|
||||
format_duration(&duration)
|
||||
);
|
||||
} else {
|
||||
debug!(
|
||||
"Client {:?} disconnected, session duration: {}",
|
||||
addr,
|
||||
format_duration(&duration)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Err(err) => {
|
||||
|
||||
Reference in New Issue
Block a user