Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds configuration for logging connections and removes get_config from entrypoint #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .circleci/pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions examples/docker/pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 18 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ 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();

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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -256,18 +258,28 @@ async fn main() {
shutdown_rx,
drain_tx,
admin_only,
tls_certificate.clone(),
config.general.log_client_connections,
)
.await
{
Ok(()) => {

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) => {
Expand Down