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

Automatically reload config every seconds (disabled by default) #86

Merged
merged 2 commits into from
Jun 25, 2022
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
3 changes: 3 additions & 0 deletions .circleci/pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ healthcheck_timeout = 100
# For how long to ban a server if it fails a health check (seconds).
ban_time = 60 # Seconds

#
autoreload = true

#
# User to use for authentication against the server.
[user]
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ healthcheck_timeout = 1000
# For how long to ban a server if it fails a health check (seconds).
ban_time = 60 # Seconds

# Reload config automatically if it changes.
autoreload = false

#
# User to use for authentication against the server.
[user]
Expand Down
19 changes: 12 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Default for User {
}

/// General configuration.
#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct General {
pub host: String,
pub port: i16,
Expand All @@ -110,6 +110,7 @@ pub struct General {
pub connect_timeout: u64,
pub healthcheck_timeout: u64,
pub ban_time: i64,
pub autoreload: bool,
}

impl Default for General {
Expand All @@ -122,6 +123,7 @@ impl Default for General {
connect_timeout: 5000,
healthcheck_timeout: 1000,
ban_time: 60,
autoreload: false,
}
}
}
Expand All @@ -143,7 +145,7 @@ impl Default for Shard {
}

/// Query Router configuration.
#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct QueryRouter {
pub default_role: String,
pub query_parser_enabled: bool,
Expand All @@ -167,7 +169,7 @@ fn default_path() -> String {
}

/// Configuration wrapper.
#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct Config {
#[serde(default = "default_path")]
pub path: String,
Expand Down Expand Up @@ -374,7 +376,7 @@ pub async fn parse(path: &str) -> Result<(), Error> {
Ok(())
}

pub async fn reload_config(client_server_map: ClientServerMap) -> Result<(), Error> {
pub async fn reload_config(client_server_map: ClientServerMap) -> Result<bool, Error> {
let old_config = get_config();

match parse(&old_config.path).await {
Expand All @@ -387,11 +389,14 @@ pub async fn reload_config(client_server_map: ClientServerMap) -> Result<(), Err

let new_config = get_config();

if old_config.shards != new_config.shards {
if old_config.shards != new_config.shards || old_config.user != new_config.user {
info!("Sharding configuration changed, re-creating server pools");
ConnectionPool::from_config(client_server_map).await
ConnectionPool::from_config(client_server_map).await?;
Ok(true)
} else if old_config != new_config {
Ok(true)
} else {
Ok(())
Ok(false)
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async fn main() {

// Save these for reloading
let reload_client_server_map = client_server_map.clone();
let autoreload_client_server_map = client_server_map.clone();

let addresses = pool.databases();
tokio::task::spawn(async move {
Expand Down Expand Up @@ -203,6 +204,26 @@ async fn main() {
}
});

if config.general.autoreload {
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(15_000));

tokio::task::spawn(async move {
info!("Config autoreloader started");

loop {
interval.tick().await;
match reload_config(autoreload_client_server_map.clone()).await {
Ok(changed) => {
if changed {
get_config().show()
}
}
Err(_) => (),
};
}
});
}

// Exit on Ctrl-C (SIGINT) and SIGTERM.
let mut term_signal = unix_signal(SignalKind::terminate()).unwrap();

Expand Down