parsec_service/utils/
global_config.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::utils::service_builder::DEFAULT_BUFFER_SIZE_LIMIT;
4use std::sync::atomic::Ordering;
5use std::sync::atomic::{AtomicBool, AtomicUsize};
6
7/// Configuration values that affect most or all the
8/// components of the service.
9#[derive(Default, Debug)]
10pub struct GlobalConfig {
11    log_error_details: AtomicBool,
12    buffer_size_limit: AtomicUsize,
13    allow_deprecated: AtomicBool,
14}
15
16impl GlobalConfig {
17    const fn new() -> Self {
18        GlobalConfig {
19            log_error_details: AtomicBool::new(false),
20            buffer_size_limit: AtomicUsize::new(DEFAULT_BUFFER_SIZE_LIMIT), // 1 MB
21            allow_deprecated: AtomicBool::new(false),
22        }
23    }
24
25    /// Determine whether error logs should include detailed
26    /// information about the error
27    pub fn log_error_details() -> bool {
28        GLOBAL_CONFIG.log_error_details.load(Ordering::Relaxed)
29    }
30
31    /// Fetch the size limit for buffers within responses (in bytes).
32    /// information about the error
33    pub fn buffer_size_limit() -> usize {
34        GLOBAL_CONFIG.buffer_size_limit.load(Ordering::Relaxed)
35    }
36
37    /// Determine whether deprecated algorithms and key types are allowed
38    /// during key generation
39    pub fn allow_deprecated() -> bool {
40        GLOBAL_CONFIG.allow_deprecated.load(Ordering::Relaxed)
41    }
42}
43
44static GLOBAL_CONFIG: GlobalConfig = GlobalConfig::new();
45
46pub(super) struct GlobalConfigBuilder {
47    log_error_details: bool,
48    buffer_size_limit: Option<usize>,
49    allow_deprecated: bool,
50}
51
52impl GlobalConfigBuilder {
53    pub fn new() -> Self {
54        GlobalConfigBuilder {
55            log_error_details: false,
56            buffer_size_limit: None,
57            allow_deprecated: false,
58        }
59    }
60
61    pub fn with_log_error_details(mut self, log_error_details: bool) -> Self {
62        self.log_error_details = log_error_details;
63
64        self
65    }
66
67    pub fn with_buffer_size_limit(mut self, buffer_size_limit: usize) -> Self {
68        self.buffer_size_limit = Some(buffer_size_limit);
69
70        self
71    }
72
73    pub fn with_allow_deprecated(mut self, allow_deprecated: bool) -> Self {
74        self.allow_deprecated = allow_deprecated;
75
76        self
77    }
78
79    pub fn build(self) {
80        GLOBAL_CONFIG
81            .log_error_details
82            .store(self.log_error_details, Ordering::Relaxed);
83        GLOBAL_CONFIG.buffer_size_limit.store(
84            self.buffer_size_limit.unwrap_or(DEFAULT_BUFFER_SIZE_LIMIT),
85            Ordering::Relaxed,
86        );
87        GLOBAL_CONFIG
88            .allow_deprecated
89            .store(self.allow_deprecated, Ordering::Relaxed);
90    }
91}