Skip to content

Commit 5bcd3bf

Browse files
authored
Automatically reload config every seconds (disabled by default) (#86)
* Automatically reload config every seconds (disabld by default) * add that
1 parent f06f641 commit 5bcd3bf

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

.circleci/pgcat.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ healthcheck_timeout = 100
2929
# For how long to ban a server if it fails a health check (seconds).
3030
ban_time = 60 # Seconds
3131

32+
#
33+
autoreload = true
34+
3235
#
3336
# User to use for authentication against the server.
3437
[user]

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pgcat.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ healthcheck_timeout = 1000
2929
# For how long to ban a server if it fails a health check (seconds).
3030
ban_time = 60 # Seconds
3131

32+
# Reload config automatically if it changes.
33+
autoreload = false
34+
3235
#
3336
# User to use for authentication against the server.
3437
[user]

src/config.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Default for User {
101101
}
102102

103103
/// General configuration.
104-
#[derive(Deserialize, Debug, Clone)]
104+
#[derive(Deserialize, Debug, Clone, PartialEq)]
105105
pub struct General {
106106
pub host: String,
107107
pub port: i16,
@@ -110,6 +110,7 @@ pub struct General {
110110
pub connect_timeout: u64,
111111
pub healthcheck_timeout: u64,
112112
pub ban_time: i64,
113+
pub autoreload: bool,
113114
}
114115

115116
impl Default for General {
@@ -122,6 +123,7 @@ impl Default for General {
122123
connect_timeout: 5000,
123124
healthcheck_timeout: 1000,
124125
ban_time: 60,
126+
autoreload: false,
125127
}
126128
}
127129
}
@@ -143,7 +145,7 @@ impl Default for Shard {
143145
}
144146

145147
/// Query Router configuration.
146-
#[derive(Deserialize, Debug, Clone)]
148+
#[derive(Deserialize, Debug, Clone, PartialEq)]
147149
pub struct QueryRouter {
148150
pub default_role: String,
149151
pub query_parser_enabled: bool,
@@ -167,7 +169,7 @@ fn default_path() -> String {
167169
}
168170

169171
/// Configuration wrapper.
170-
#[derive(Deserialize, Debug, Clone)]
172+
#[derive(Deserialize, Debug, Clone, PartialEq)]
171173
pub struct Config {
172174
#[serde(default = "default_path")]
173175
pub path: String,
@@ -374,7 +376,7 @@ pub async fn parse(path: &str) -> Result<(), Error> {
374376
Ok(())
375377
}
376378

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

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

388390
let new_config = get_config();
389391

390-
if old_config.shards != new_config.shards {
392+
if old_config.shards != new_config.shards || old_config.user != new_config.user {
391393
info!("Sharding configuration changed, re-creating server pools");
392-
ConnectionPool::from_config(client_server_map).await
394+
ConnectionPool::from_config(client_server_map).await?;
395+
Ok(true)
396+
} else if old_config != new_config {
397+
Ok(true)
393398
} else {
394-
Ok(())
399+
Ok(false)
395400
}
396401
}
397402

src/main.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ async fn main() {
127127

128128
// Save these for reloading
129129
let reload_client_server_map = client_server_map.clone();
130+
let autoreload_client_server_map = client_server_map.clone();
130131

131132
let addresses = pool.databases();
132133
tokio::task::spawn(async move {
@@ -203,6 +204,26 @@ async fn main() {
203204
}
204205
});
205206

207+
if config.general.autoreload {
208+
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(15_000));
209+
210+
tokio::task::spawn(async move {
211+
info!("Config autoreloader started");
212+
213+
loop {
214+
interval.tick().await;
215+
match reload_config(autoreload_client_server_map.clone()).await {
216+
Ok(changed) => {
217+
if changed {
218+
get_config().show()
219+
}
220+
}
221+
Err(_) => (),
222+
};
223+
}
224+
});
225+
}
226+
206227
// Exit on Ctrl-C (SIGINT) and SIGTERM.
207228
let mut term_signal = unix_signal(SignalKind::terminate()).unwrap();
208229

0 commit comments

Comments
 (0)