lib.rs (3027B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024, 2025, 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::{path::PathBuf, time::Duration}; 18 19 use config::{Config, parser::ConfigSource}; 20 use tracing::error; 21 use tracing_subscriber::util::SubscriberInitExt; 22 23 use crate::log::taler_logger; 24 25 pub mod api_common; 26 pub mod api_params; 27 pub mod api_revenue; 28 pub mod api_transfer; 29 pub mod api_wire; 30 pub mod bench; 31 pub mod cli; 32 pub mod config; 33 pub mod db; 34 pub mod error; 35 pub mod error_code; 36 pub mod json_file; 37 pub mod log; 38 pub mod types; 39 40 #[derive(clap::Parser, Debug, Clone)] 41 pub struct CommonArgs { 42 /// Specifies the configuration file 43 #[clap(long, short)] 44 #[arg(global = true)] 45 config: Option<PathBuf>, 46 47 /// Configure logging to use LOGLEVEL 48 #[clap(long, short('L'))] 49 #[arg(global = true)] 50 log: Option<tracing::Level>, 51 } 52 53 pub fn taler_main( 54 src: ConfigSource, 55 args: CommonArgs, 56 app: impl AsyncFnOnce(Config) -> Result<(), anyhow::Error>, 57 ) { 58 taler_logger(args.log).init(); 59 let cfg = match Config::from_file(src, args.config) { 60 Ok(cfg) => cfg, 61 Err(err) => { 62 error!(target: "config", "{}", err); 63 std::process::exit(1); 64 } 65 }; 66 67 // Setup async runtime 68 let runtime = tokio::runtime::Builder::new_multi_thread() 69 .enable_all() 70 .build() 71 .unwrap(); 72 73 // Run app 74 let result = runtime.block_on(app(cfg)); 75 if let Err(err) = result { 76 error!(target: "cli", "{}", err); 77 std::process::exit(1); 78 } 79 } 80 81 /// Infinite exponential backoff with decorrelated jitter 82 pub struct ExpoBackoffDecorr { 83 base: u32, 84 max: u32, 85 factor: f32, 86 sleep: u32, 87 } 88 89 impl ExpoBackoffDecorr { 90 pub fn new(base: Duration, max: Duration, factor: f32) -> Self { 91 Self { 92 base: base.as_millis() as u32, 93 max: max.as_millis() as u32, 94 factor, 95 sleep: base.as_millis() as u32, 96 } 97 } 98 99 pub fn backoff(&mut self) -> Duration { 100 self.sleep = 101 rand::random_range(self.base..(self.sleep as f32 * self.factor) as u32).min(self.max); 102 Duration::from_millis(self.sleep as u64) 103 } 104 105 pub fn reset(&mut self) { 106 self.sleep = self.base 107 } 108 } 109 110 impl Default for ExpoBackoffDecorr { 111 fn default() -> Self { 112 Self::new(Duration::from_millis(400), Duration::from_secs(30), 2.5) 113 } 114 }