From e1a0ab7d25bd1b48f7e02297d5efd22cf7aef463 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sat, 13 May 2023 05:22:28 -0400 Subject: [PATCH 1/4] use thiserror for impl Error and Display --- packages/backend/Cargo.toml | 4 +++ packages/backend/crates/config/Cargo.toml | 1 + packages/backend/crates/config/src/lib.rs | 43 ++++------------------- packages/backend/src/main.rs | 32 +++++++++++++---- 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/packages/backend/Cargo.toml b/packages/backend/Cargo.toml index 1d2e91c2e8..442febff54 100644 --- a/packages/backend/Cargo.toml +++ b/packages/backend/Cargo.toml @@ -23,5 +23,9 @@ queue = { path = "crates/queue" } config = { path = "crates/config" } macros = { path = "crates/macros" } lazy_static = "1.4.0" +tracing = "0.1.37" +tracing-subscriber = "0.3.17" +tokio = { version = "1.28.1", features = ["full"] } +anyhow = "1.0.71" [dev-dependencies] diff --git a/packages/backend/crates/config/Cargo.toml b/packages/backend/crates/config/Cargo.toml index 0058bdbf2e..9214c74976 100644 --- a/packages/backend/crates/config/Cargo.toml +++ b/packages/backend/crates/config/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" once_cell = "1.17.1" serde = { version = "1.0.160", features = [ "derive" ] } serde_yaml = "0.9.21" +thiserror = "1.0.40" diff --git a/packages/backend/crates/config/src/lib.rs b/packages/backend/crates/config/src/lib.rs index e825137f47..ba1c3ec461 100644 --- a/packages/backend/crates/config/src/lib.rs +++ b/packages/backend/crates/config/src/lib.rs @@ -1,4 +1,3 @@ -use std::fmt::Display; use std::fs::File; use std::io::{self, Read}; use std::path::Path; @@ -11,44 +10,16 @@ mod data; pub use data::*; // Config Errors -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub enum Error { + #[error("The configuration has not been initialized yet")] Uninitialized, - Deserialize(serde_yaml::Error), - FileError(io::Error), + #[error("Error when parsing config file: {0}")] + Deserialize(#[from] serde_yaml::Error), + #[error("Error when reading config file: {0}")] + FileError(#[from] io::Error), } -macro_rules! generate_error_impl { - ($t:ident, $o:ty) => { - impl From<$o> for Error { - fn from(value: $o) -> Self { - Self::$t(value) - } - } - }; -} - -impl Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use Error::*; - - f.write_str(&{ - match self { - Uninitialized => { - format!("The configuration has not been initialized yet: {:?}", self) - } - Deserialize(e) => format!("Error when parsing config file: {}", e), - FileError(e) => format!("Error when reading config file: {}", e), - } - }) - } -} - -generate_error_impl!(FileError, io::Error); -generate_error_impl!(Deserialize, serde_yaml::Error); - -impl std::error::Error for Error {} - // Functions fn fetch_config(path: &Path) -> Result { let mut buf = String::new(); @@ -153,7 +124,7 @@ redis: max_note_length: MaxNoteLength(3000), max_caption_length: MaxCommentLength(1500), cluster_limit: None, - env: Environment { }, + env: Environment {}, } ); } diff --git a/packages/backend/src/main.rs b/packages/backend/src/main.rs index 760391f136..c9a2cc95d0 100644 --- a/packages/backend/src/main.rs +++ b/packages/backend/src/main.rs @@ -1,12 +1,14 @@ use std::{ - env, error, fmt, + env, fmt, path::{Path, PathBuf}, }; +use tracing::debug; #[macro_use] extern crate macros; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> anyhow::Result<()> { env::set_var( "CK_REPO_DIR", PathBuf::from(env!("PWD")) @@ -14,17 +16,33 @@ fn main() -> Result<(), Box> { .and_then(|p| p.parent()) .ok_or(fmt::Error)?, ); + + // logging + let is_debug = match env::var_os("NODE_ENV") { + None => true, + Some(val) => val != "production", + }; + let subscriber = tracing_subscriber::fmt(); + if is_debug { + subscriber + .with_max_level(tracing::Level::DEBUG) + .pretty() + .init(); + } else { + subscriber.with_max_level(tracing::Level::INFO).init(); + } + // bootstrap // ENV // get config - config::init_config( - &Path::new(&env::var("CK_REPO_DIR")?) - .join(".config") - .join("default.yml"), - )?; + let config_path = &Path::new(&env::var("CK_REPO_DIR")?) + .join(".config") + .join("default.yml"); + debug!(target: "config", path = ?config_path, "Loading yaml file"); + config::init_config(config_path)?; eprintln!("{:?}", config::get_config()?); From bee531ddca6f131feab458dc4addb71b40cf3414 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sat, 13 May 2023 22:58:03 -0400 Subject: [PATCH 2/4] remove tokio main --- packages/backend/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/backend/src/main.rs b/packages/backend/src/main.rs index c9a2cc95d0..4daf3c84bd 100644 --- a/packages/backend/src/main.rs +++ b/packages/backend/src/main.rs @@ -7,8 +7,7 @@ use tracing::debug; #[macro_use] extern crate macros; -#[tokio::main] -async fn main() -> anyhow::Result<()> { +fn main() -> anyhow::Result<()> { env::set_var( "CK_REPO_DIR", PathBuf::from(env!("PWD")) From e69f3b54a9febcf46c0cd8a63d33caaa317c6e69 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 14 May 2023 03:30:10 -0400 Subject: [PATCH 3/4] add default values --- packages/backend/crates/config/src/data.rs | 50 +++++++++++++++++++--- packages/backend/crates/config/src/lib.rs | 8 +++- packages/backend/src/main.rs | 10 ++--- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/packages/backend/crates/config/src/data.rs b/packages/backend/crates/config/src/data.rs index 8553f21858..4048329e90 100644 --- a/packages/backend/crates/config/src/data.rs +++ b/packages/backend/crates/config/src/data.rs @@ -112,13 +112,20 @@ pub struct Config { #[serde(default)] pub max_caption_length: MaxCommentLength, // pub disable_hsts: bool, - pub cluster_limit: Option, - // pub deliver_job_concurrency: u16, - // pub inbox_job_concurrency: u16, - // pub deliver_job_per_sec: u16, - // pub inbox_job_per_sec: u16, - // pub deliver_job_max_attempts: u16, - // pub inbox_job_max_attempts: u16, + #[serde(default = "cluster_limit_default")] + pub cluster_limit: u16, + #[serde(default = "deliver_job_default")] + pub deliver_job_concurrency: u16, + #[serde(default = "inbox_job_default")] + pub inbox_job_concurrency: u16, + #[serde(default = "deliver_job_default")] + pub deliver_job_per_sec: u16, + #[serde(default = "inbox_job_default")] + pub inbox_job_per_sec: u16, + #[serde(default = "deliver_job_attempts_default")] + pub deliver_job_max_attempts: u16, + #[serde(default = "inbox_job_attempts_default")] + pub inbox_job_max_attempts: u16, // pub outgoing_address_family: IpFamily, // pub syslog: syslog::SyslogConfig, // pub proxy: Option, @@ -182,6 +189,8 @@ pub mod db { /// redis config pub mod redis { + use url::Url; + use super::*; #[derive(Debug, PartialEq, Deserialize)] @@ -196,6 +205,13 @@ pub mod redis { #[serde(default)] pub db: u8, } + + impl From<&RedisConfig> for Url { + fn from(value: &RedisConfig) -> Self { + Url::parse(&format!("redis://{}:{}", value.host.1, value.port)) + .expect("Invalid redis host and port") + } + } } /// sonic search config @@ -270,6 +286,26 @@ impl Default for MaxCommentLength { } } +fn cluster_limit_default() -> u16 { + 1 +} + +fn deliver_job_default() -> u16 { + 128 +} + +fn deliver_job_attempts_default() -> u16 { + 12 +} + +fn inbox_job_default() -> u16 { + 16 +} + +fn inbox_job_attempts_default() -> u16 { + 8 +} + fn true_fn() -> bool { true } diff --git a/packages/backend/crates/config/src/lib.rs b/packages/backend/crates/config/src/lib.rs index ba1c3ec461..b8830480e9 100644 --- a/packages/backend/crates/config/src/lib.rs +++ b/packages/backend/crates/config/src/lib.rs @@ -123,8 +123,14 @@ redis: }, max_note_length: MaxNoteLength(3000), max_caption_length: MaxCommentLength(1500), - cluster_limit: None, + cluster_limit: 1, env: Environment {}, + deliver_job_concurrency: 128, + inbox_job_concurrency: 16, + deliver_job_per_sec: 128, + inbox_job_per_sec: 16, + deliver_job_max_attempts: 12, + inbox_job_max_attempts: 8, } ); } diff --git a/packages/backend/src/main.rs b/packages/backend/src/main.rs index 4daf3c84bd..5d9a9d6a3d 100644 --- a/packages/backend/src/main.rs +++ b/packages/backend/src/main.rs @@ -17,18 +17,14 @@ fn main() -> anyhow::Result<()> { ); // logging - let is_debug = match env::var_os("NODE_ENV") { - None => true, - Some(val) => val != "production", - }; let subscriber = tracing_subscriber::fmt(); - if is_debug { + if is_release!() { + subscriber.with_max_level(tracing::Level::INFO).init(); + } else { subscriber .with_max_level(tracing::Level::DEBUG) .pretty() .init(); - } else { - subscriber.with_max_level(tracing::Level::INFO).init(); } // bootstrap From 04a9dc8c97f22143f8df59137817edf3609595e6 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 14 May 2023 04:09:05 -0400 Subject: [PATCH 4/4] add url --- packages/backend/crates/config/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/backend/crates/config/Cargo.toml b/packages/backend/crates/config/Cargo.toml index 9214c74976..dc5b28076d 100644 --- a/packages/backend/crates/config/Cargo.toml +++ b/packages/backend/crates/config/Cargo.toml @@ -10,4 +10,5 @@ once_cell = "1.17.1" serde = { version = "1.0.160", features = [ "derive" ] } serde_yaml = "0.9.21" thiserror = "1.0.40" +url = "2.3.1"