From 2d8a99c2fc24eec255bfcb66aeebfdf25f795d25 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 10 Nov 2020 10:13:51 +0100 Subject: [PATCH] Add configurable timeout and log file directory Signed-off-by: trivernis --- .gitignore | 3 +- Cargo.lock | 14 +++++++-- Cargo.toml | 5 +-- src/modules/heartbeat/mod.rs | 13 ++++++-- src/server/mod.rs | 12 ++++++-- src/utils/logging.rs | 60 +++++++++++++++++++++++++++--------- src/utils/settings.rs | 17 ++++++++++ 7 files changed, 98 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index f9f9632..648c1ef 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.env config nodes -testdata \ No newline at end of file +testdata +logs \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 1f81e89..737a422 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,6 +292,15 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" +[[package]] +name = "fern" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9a4820f0ccc8a7afd67c39a0f1a0f4b07ca1725164271a64939d7aeb9af065" +dependencies = [ + "log", +] + [[package]] name = "generic-array" version = "0.14.4" @@ -859,6 +868,7 @@ dependencies = [ "colored", "config", "env_logger", + "fern", "glob", "hostname", "lazy_static", @@ -1036,9 +1046,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "vented" -version = "0.10.4" +version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ce039664251094f5b55eee04f5eb82c7e13abca8df6d03601fa975e9fb0a2bf" +checksum = "7465a4d13f2b81be52d2902753c7a47edd342e6b398a23df73109f32cdad59c9" dependencies = [ "byteorder", "crossbeam-channel", diff --git a/Cargo.toml b/Cargo.toml index 0a9d968..1ccddac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -vented = "0.10.4" +vented = "0.10.5" rusqlite = "0.24.1" rand = "0.7.3" base64 = "0.13.0" @@ -26,4 +26,5 @@ scheduled-thread-pool = "0.2.5" num_cpus = "1.13.0" lazy_static = "1.4.0" parking_lot = "0.11.0" -serde_json = "1.0.59" \ No newline at end of file +serde_json = "1.0.59" +fern = "0.6.0" \ No newline at end of file diff --git a/src/modules/heartbeat/mod.rs b/src/modules/heartbeat/mod.rs index 9085cbb..06c2296 100644 --- a/src/modules/heartbeat/mod.rs +++ b/src/modules/heartbeat/mod.rs @@ -127,9 +127,16 @@ impl Module for HeartbeatModule { ); let states = Arc::clone(&self.node_states); pool.execute(move || { - if let Some(Err(e)) = future.get_value_with_timeout(Duration::from_secs(10)) { - log::debug!("Node {} is not reachable: {}", node.id, e); - Self::insert_state(&mut states.lock(), node.id, NodeInfo::dead()); + match future.get_value_with_timeout(Duration::from_secs(60)) { + Some(Err(e)) => { + log::debug!("Node {} is not reachable: {}", node.id, e); + Self::insert_state(&mut states.lock(), node.id, NodeInfo::dead()); + } + None => { + log::debug!("Node {} is not reachable: Timeout", node.id); + Self::insert_state(&mut states.lock(), node.id, NodeInfo::dead()); + } + _ => {} } }); } diff --git a/src/server/mod.rs b/src/server/mod.rs index c097427..a05d526 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,9 +1,9 @@ use crate::modules::Module; use crate::server::tick_context::TickContext; use crate::utils::result::{SnekcloudError, SnekcloudResult}; +use crate::utils::settings::get_settings; use parking_lot::Mutex; use scheduled_thread_pool::ScheduledThreadPool; -use std::cmp::max; use std::collections::HashMap; use std::mem; use std::sync::mpsc::channel; @@ -30,9 +30,15 @@ pub struct SnekcloudServer { impl SnekcloudServer { /// Creates a new snekcloud server with the provided keys and number of threads pub fn new(id: String, private_key: SecretKey, keys: Vec, num_threads: usize) -> Self { - let num_threads = max(num_threads, keys.len()); Self { - inner: VentedServer::new(id, private_key, keys, num_threads * 2, num_threads * 10), + inner: VentedServer::new( + id, + private_key, + keys, + get_settings().timeouts(), + num_threads * 2, + num_threads * 10, + ), listen_addresses: Vec::new(), listeners: Vec::new(), module_pool: HashMap::new(), diff --git a/src/utils/logging.rs b/src/utils/logging.rs index 2d6ba20..a866484 100644 --- a/src/utils/logging.rs +++ b/src/utils/logging.rs @@ -1,21 +1,34 @@ -use log::Level; -use std::thread; -use env_logger::Env; +use crate::utils::settings::get_settings; use chrono::Local; use colored::*; +use log::{Level, LevelFilter}; +use std::fs; +use std::path::PathBuf; +use std::str::FromStr; +use std::thread; /// Initializes the env_logger with a custom format /// that also logs the thread names pub fn init_logger() { - env_logger::Builder::from_env(Env::default().default_filter_or("info")) - .format(|buf, record| { - use std::io::Write; + let log_dir = get_settings().log_folder; + if !log_dir.exists() { + fs::create_dir(&log_dir).expect("failed to create log dir"); + } + fern::Dispatch::new() + .format(|out, message, record| { let color = get_level_style(record.level()); - writeln!( - buf, - "{:<25} {:<45}| {} {}: {}", - format!("thread::{}", thread::current().name().unwrap_or("main")).dimmed(), - record.target().dimmed().italic(), + let mut thread_name = format!( + "thread::{}", + thread::current().name().unwrap_or("main").to_string() + ); + thread_name.truncate(34); + let mut target = record.target().to_string(); + target.truncate(39); + + out.finish(format_args!( + "{:<35} {:<40}| {} {}: {}", + thread_name.dimmed(), + target.dimmed().italic(), Local::now().format("%Y-%m-%dT%H:%M:%S"), record .level() @@ -23,10 +36,27 @@ pub fn init_logger() { .to_lowercase() .as_str() .color(color), - record.args() - ) + message + )) }) - .init(); + .level( + log::LevelFilter::from_str( + std::env::var("RUST_LOG") + .unwrap_or("info".to_string()) + .as_str(), + ) + .unwrap_or(LevelFilter::Info), + ) + .chain(std::io::stdout()) + .chain( + fern::log_file(log_dir.join(PathBuf::from(format!( + "{}.log", + Local::now().format("%Y-%m-%d"), + )))) + .expect("failed to create log file"), + ) + .apply() + .expect("failed to init logger"); } fn get_level_style(level: Level) -> colored::Color { @@ -37,4 +67,4 @@ fn get_level_style(level: Level) -> colored::Color { Level::Warn => colored::Color::Yellow, Level::Error => colored::Color::Red, } -} \ No newline at end of file +} diff --git a/src/utils/settings.rs b/src/utils/settings.rs index a137ddf..07d54b3 100644 --- a/src/utils/settings.rs +++ b/src/utils/settings.rs @@ -6,6 +6,8 @@ use config::File; use serde::{Deserialize, Serialize}; use std::fs; use std::path::{Path, PathBuf}; +use std::time::Duration; +use vented::server::data::ServerTimeouts; const CONFIG_DIR: &str = "config/"; const DEFAULT_CONFIG: &str = "config/00_default.toml"; @@ -21,6 +23,9 @@ pub struct Settings { pub num_threads: usize, /// List of trusted nodes pub trusted_nodes: Vec, + pub send_timeout_secs: u64, + pub redirect_timeout_secs: u64, + pub log_folder: PathBuf, // modules need to be last because it's a table pub modules: ModuleSettings, } @@ -38,8 +43,11 @@ impl Default for Settings { node_id: get_node_id(), private_key: PathBuf::from("node_key"), node_data_dir: PathBuf::from("nodes"), + log_folder: PathBuf::from("logs"), trusted_nodes: vec![], num_threads: num_cpus::get(), + send_timeout_secs: 5, + redirect_timeout_secs: 20, modules: ModuleSettings::default(), } } @@ -81,3 +89,12 @@ fn load_settings() -> SnekcloudResult { settings.try_into().map_err(SnekcloudError::from) } + +impl Settings { + pub fn timeouts(&self) -> ServerTimeouts { + ServerTimeouts { + redirect_timeout: Duration::from_secs(self.redirect_timeout_secs), + send_timeout: Duration::from_secs(self.send_timeout_secs), + } + } +}