taler-rust

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

lib.rs (3151B)


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