taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

lib.rs (3122B)


      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 mimalloc::MiMalloc;
     21 use tracing::error;
     22 use tracing_subscriber::util::SubscriberInitExt;
     23 
     24 use crate::log::taler_logger;
     25 
     26 pub mod api;
     27 pub mod bench;
     28 pub mod cli;
     29 pub mod config;
     30 pub mod db;
     31 pub mod encoding;
     32 pub mod error;
     33 pub mod error_code;
     34 pub mod json_file;
     35 pub mod log;
     36 pub mod types;
     37 
     38 #[global_allocator]
     39 static GLOBAL: MiMalloc = MiMalloc;
     40 
     41 #[derive(clap::Parser, Debug, Clone)]
     42 pub struct CommonArgs {
     43     /// Specifies the configuration file
     44     #[arg(short, long, global = true)]
     45     config: Option<PathBuf>,
     46 
     47     /// Configure logging to use LOGLEVEL
     48     #[arg(short('L'), long, global = true)]
     49     log: Option<tracing::Level>,
     50 
     51     /// Show logs from all sources
     52     #[arg(short, long, global = true)]
     53     verbose: bool,
     54 }
     55 
     56 pub fn taler_main(
     57     src: ConfigSource,
     58     args: CommonArgs,
     59     app: impl AsyncFnOnce(Config) -> Result<(), anyhow::Error>,
     60 ) {
     61     taler_logger(args.log, args.verbose).init();
     62     let cfg = match Config::from_file(src, args.config) {
     63         Ok(cfg) => cfg,
     64         Err(err) => {
     65             error!(target: "config", "{}", err);
     66             std::process::exit(1);
     67         }
     68     };
     69 
     70     // Setup async runtime
     71     let runtime = tokio::runtime::Builder::new_multi_thread()
     72         .enable_all()
     73         .build()
     74         .unwrap();
     75 
     76     // Run app
     77     let result = runtime.block_on(app(cfg));
     78     if let Err(err) = result {
     79         error!(target: "cli", "{}", err);
     80         std::process::exit(1);
     81     }
     82 }
     83 
     84 /// Infinite exponential backoff with decorrelated jitter
     85 pub struct ExpoBackoffDecorr {
     86     base: u32,
     87     max: u32,
     88     factor: f32,
     89     sleep: u32,
     90 }
     91 
     92 impl ExpoBackoffDecorr {
     93     pub fn new(base: Duration, max: Duration, factor: f32) -> Self {
     94         Self {
     95             base: base.as_millis() as u32,
     96             max: max.as_millis() as u32,
     97             factor,
     98             sleep: base.as_millis() as u32,
     99         }
    100     }
    101 
    102     pub fn backoff(&mut self) -> Duration {
    103         self.sleep =
    104             rand::random_range(self.base..(self.sleep as f32 * self.factor) as u32).min(self.max);
    105         Duration::from_millis(self.sleep as u64)
    106     }
    107 
    108     pub fn reset(&mut self) {
    109         self.sleep = self.base
    110     }
    111 }
    112 
    113 impl Default for ExpoBackoffDecorr {
    114     fn default() -> Self {
    115         Self::new(Duration::from_millis(400), Duration::from_secs(30), 2.5)
    116     }
    117 }