Add configurable timeout and log file directory

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 69b9e98ebb
commit 2d8a99c2fc
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

3
.gitignore vendored

@ -3,4 +3,5 @@
*.env *.env
config config
nodes nodes
testdata testdata
logs

14
Cargo.lock generated

@ -292,6 +292,15 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "fern"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9a4820f0ccc8a7afd67c39a0f1a0f4b07ca1725164271a64939d7aeb9af065"
dependencies = [
"log",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.4" version = "0.14.4"
@ -859,6 +868,7 @@ dependencies = [
"colored", "colored",
"config", "config",
"env_logger", "env_logger",
"fern",
"glob", "glob",
"hostname", "hostname",
"lazy_static", "lazy_static",
@ -1036,9 +1046,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "vented" name = "vented"
version = "0.10.4" version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ce039664251094f5b55eee04f5eb82c7e13abca8df6d03601fa975e9fb0a2bf" checksum = "7465a4d13f2b81be52d2902753c7a47edd342e6b398a23df73109f32cdad59c9"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"crossbeam-channel", "crossbeam-channel",

@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
vented = "0.10.4" vented = "0.10.5"
rusqlite = "0.24.1" rusqlite = "0.24.1"
rand = "0.7.3" rand = "0.7.3"
base64 = "0.13.0" base64 = "0.13.0"
@ -26,4 +26,5 @@ scheduled-thread-pool = "0.2.5"
num_cpus = "1.13.0" num_cpus = "1.13.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
parking_lot = "0.11.0" parking_lot = "0.11.0"
serde_json = "1.0.59" serde_json = "1.0.59"
fern = "0.6.0"

@ -127,9 +127,16 @@ impl Module for HeartbeatModule {
); );
let states = Arc::clone(&self.node_states); let states = Arc::clone(&self.node_states);
pool.execute(move || { pool.execute(move || {
if let Some(Err(e)) = future.get_value_with_timeout(Duration::from_secs(10)) { match future.get_value_with_timeout(Duration::from_secs(60)) {
log::debug!("Node {} is not reachable: {}", node.id, e); Some(Err(e)) => {
Self::insert_state(&mut states.lock(), node.id, NodeInfo::dead()); 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());
}
_ => {}
} }
}); });
} }

@ -1,9 +1,9 @@
use crate::modules::Module; use crate::modules::Module;
use crate::server::tick_context::TickContext; use crate::server::tick_context::TickContext;
use crate::utils::result::{SnekcloudError, SnekcloudResult}; use crate::utils::result::{SnekcloudError, SnekcloudResult};
use crate::utils::settings::get_settings;
use parking_lot::Mutex; use parking_lot::Mutex;
use scheduled_thread_pool::ScheduledThreadPool; use scheduled_thread_pool::ScheduledThreadPool;
use std::cmp::max;
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
@ -30,9 +30,15 @@ pub struct SnekcloudServer {
impl SnekcloudServer { impl SnekcloudServer {
/// Creates a new snekcloud server with the provided keys and number of threads /// Creates a new snekcloud server with the provided keys and number of threads
pub fn new(id: String, private_key: SecretKey, keys: Vec<Node>, num_threads: usize) -> Self { pub fn new(id: String, private_key: SecretKey, keys: Vec<Node>, num_threads: usize) -> Self {
let num_threads = max(num_threads, keys.len());
Self { 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(), listen_addresses: Vec::new(),
listeners: Vec::new(), listeners: Vec::new(),
module_pool: HashMap::new(), module_pool: HashMap::new(),

@ -1,21 +1,34 @@
use log::Level; use crate::utils::settings::get_settings;
use std::thread;
use env_logger::Env;
use chrono::Local; use chrono::Local;
use colored::*; 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 /// Initializes the env_logger with a custom format
/// that also logs the thread names /// that also logs the thread names
pub fn init_logger() { pub fn init_logger() {
env_logger::Builder::from_env(Env::default().default_filter_or("info")) let log_dir = get_settings().log_folder;
.format(|buf, record| { if !log_dir.exists() {
use std::io::Write; 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()); let color = get_level_style(record.level());
writeln!( let mut thread_name = format!(
buf, "thread::{}",
"{:<25} {:<45}| {} {}: {}", thread::current().name().unwrap_or("main").to_string()
format!("thread::{}", thread::current().name().unwrap_or("main")).dimmed(), );
record.target().dimmed().italic(), 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"), Local::now().format("%Y-%m-%dT%H:%M:%S"),
record record
.level() .level()
@ -23,10 +36,27 @@ pub fn init_logger() {
.to_lowercase() .to_lowercase()
.as_str() .as_str()
.color(color), .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 { 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::Warn => colored::Color::Yellow,
Level::Error => colored::Color::Red, Level::Error => colored::Color::Red,
} }
} }

@ -6,6 +6,8 @@ use config::File;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Duration;
use vented::server::data::ServerTimeouts;
const CONFIG_DIR: &str = "config/"; const CONFIG_DIR: &str = "config/";
const DEFAULT_CONFIG: &str = "config/00_default.toml"; const DEFAULT_CONFIG: &str = "config/00_default.toml";
@ -21,6 +23,9 @@ pub struct Settings {
pub num_threads: usize, pub num_threads: usize,
/// List of trusted nodes /// List of trusted nodes
pub trusted_nodes: Vec<String>, pub trusted_nodes: Vec<String>,
pub send_timeout_secs: u64,
pub redirect_timeout_secs: u64,
pub log_folder: PathBuf,
// modules need to be last because it's a table // modules need to be last because it's a table
pub modules: ModuleSettings, pub modules: ModuleSettings,
} }
@ -38,8 +43,11 @@ impl Default for Settings {
node_id: get_node_id(), node_id: get_node_id(),
private_key: PathBuf::from("node_key"), private_key: PathBuf::from("node_key"),
node_data_dir: PathBuf::from("nodes"), node_data_dir: PathBuf::from("nodes"),
log_folder: PathBuf::from("logs"),
trusted_nodes: vec![], trusted_nodes: vec![],
num_threads: num_cpus::get(), num_threads: num_cpus::get(),
send_timeout_secs: 5,
redirect_timeout_secs: 20,
modules: ModuleSettings::default(), modules: ModuleSettings::default(),
} }
} }
@ -81,3 +89,12 @@ fn load_settings() -> SnekcloudResult<Settings> {
settings.try_into().map_err(SnekcloudError::from) 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),
}
}
}

Loading…
Cancel
Save