From 9643c9dcebcc82b65eb8ec3a4817f53a0e724e96 Mon Sep 17 00:00:00 2001 From: Mostafa Abdelraouf Date: Fri, 19 Aug 2022 22:18:46 -0500 Subject: [PATCH] Minor refactor to decouple config from the rest of the app --- src/admin.rs | 4 ++-- src/config.rs | 23 ----------------------- src/main.rs | 26 +++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/admin.rs b/src/admin.rs index 6a79e49e..4718169d 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -3,12 +3,12 @@ use bytes::{Buf, BufMut, BytesMut}; use log::{info, trace}; use std::collections::HashMap; -use crate::config::{get_config, reload_config, VERSION}; +use crate::config::{get_config, VERSION}; use crate::errors::Error; -use crate::messages::*; use crate::pool::get_all_pools; use crate::stats::get_stats; use crate::ClientServerMap; +use crate::{messages::*, reload_config}; pub fn generate_server_info_for_admin() -> BytesMut { let mut server_info = BytesMut::new(); diff --git a/src/config.rs b/src/config.rs index ed338104..a1afdbcc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,6 @@ use toml; use crate::errors::Error; use crate::tls::{load_certs, load_keys}; -use crate::{ClientServerMap, ConnectionPool}; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -525,28 +524,6 @@ pub async fn parse(path: &str) -> Result<(), Error> { Ok(()) } -pub async fn reload_config(client_server_map: ClientServerMap) -> Result { - let old_config = get_config(); - match parse(&old_config.path).await { - Ok(()) => (), - Err(err) => { - error!("Config reload error: {:?}", err); - return Err(Error::BadConfig); - } - }; - let new_config = get_config(); - - if old_config.pools != new_config.pools { - info!("Pool configuration changed, re-creating server pools"); - ConnectionPool::from_config(client_server_map).await?; - Ok(true) - } else if old_config != new_config { - Ok(true) - } else { - Ok(false) - } -} - #[cfg(test)] mod test { use super::*; diff --git a/src/main.rs b/src/main.rs index 0b2e1d59..d49e16bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ extern crate tokio; extern crate tokio_rustls; extern crate toml; +use config::parse; use log::{debug, error, info}; use parking_lot::Mutex; use tokio::net::TcpListener; @@ -65,7 +66,8 @@ mod sharding; mod stats; mod tls; -use crate::config::{get_config, reload_config, VERSION}; +use crate::config::{get_config, VERSION}; +use crate::errors::Error; use crate::pool::{ClientServerMap, ConnectionPool}; use crate::prometheus::start_metric_server; use crate::stats::{Collector, Reporter, REPORTER}; @@ -340,3 +342,25 @@ fn format_duration(duration: &chrono::Duration) -> String { format!("{}d {}:{}:{}", days, hours, minutes, seconds) } + +async fn reload_config(client_server_map: ClientServerMap) -> Result { + let old_config = get_config(); + match parse(&old_config.path).await { + Ok(()) => (), + Err(err) => { + error!("Config reload error: {:?}", err); + return Err(Error::BadConfig); + } + }; + let new_config = get_config(); + + if old_config.pools != new_config.pools { + info!("Pool configuration changed, re-creating server pools"); + ConnectionPool::from_config(client_server_map).await?; + Ok(true) + } else if old_config != new_config { + Ok(true) + } else { + Ok(false) + } +}