From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 55A6D1FF56B for ; Mon, 22 Apr 2024 14:38:52 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7368F10E86; Mon, 22 Apr 2024 14:38:52 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com Date: Mon, 22 Apr 2024 14:38:00 +0200 Message-Id: <20240422123841.280675-3-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240422123841.280675-1-l.wagner@proxmox.com> References: <20240422123841.280675-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.001 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox v4 02/43] notify: use std::sync::OnceCell instead of lazy_static! X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" Signed-off-by: Lukas Wagner --- proxmox-notify/Cargo.toml | 1 - proxmox-notify/src/config.rs | 23 +++++++++++++++-------- proxmox-notify/src/lib.rs | 5 ++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/proxmox-notify/Cargo.toml b/proxmox-notify/Cargo.toml index 797b1ac..3a97847 100644 --- a/proxmox-notify/Cargo.toml +++ b/proxmox-notify/Cargo.toml @@ -11,7 +11,6 @@ exclude.workspace = true anyhow.workspace = true const_format.workspace = true handlebars = { workspace = true } -lazy_static.workspace = true lettre = { workspace = true, optional = true } log.workspace = true mail-parser = { workspace = true, optional = true } diff --git a/proxmox-notify/src/config.rs b/proxmox-notify/src/config.rs index fe25ea7..4445371 100644 --- a/proxmox-notify/src/config.rs +++ b/proxmox-notify/src/config.rs @@ -1,4 +1,4 @@ -use lazy_static::lazy_static; +use std::sync::OnceLock; use proxmox_schema::{ApiType, ObjectSchema}; use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; @@ -9,9 +9,16 @@ use crate::matcher::{MatcherConfig, MATCHER_TYPENAME}; use crate::schema::BACKEND_NAME_SCHEMA; use crate::Error; -lazy_static! { - pub static ref CONFIG: SectionConfig = config_init(); - pub static ref PRIVATE_CONFIG: SectionConfig = private_config_init(); +/// Section config schema for the public config file. +pub fn config_parser() -> &'static SectionConfig { + static CONFIG: OnceLock = OnceLock::new(); + CONFIG.get_or_init(|| config_init()) +} + +/// Section config schema for the private config file. +pub fn private_config_parser() -> &'static SectionConfig { + static CONFIG: OnceLock = OnceLock::new(); + CONFIG.get_or_init(|| private_config_init()) } fn config_init() -> SectionConfig { @@ -108,7 +115,7 @@ fn private_config_init() -> SectionConfig { pub fn config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> { let digest = openssl::sha::sha256(raw_config.as_bytes()); - let mut data = CONFIG + let mut data = config_parser() .parse("notifications.cfg", raw_config) .map_err(|err| Error::ConfigDeserialization(err.into()))?; @@ -139,20 +146,20 @@ pub fn config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> pub fn private_config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> { let digest = openssl::sha::sha256(raw_config.as_bytes()); - let data = PRIVATE_CONFIG + let data = private_config_parser() .parse("priv/notifications.cfg", raw_config) .map_err(|err| Error::ConfigDeserialization(err.into()))?; Ok((data, digest)) } pub fn write(config: &SectionConfigData) -> Result { - CONFIG + config_parser() .write("notifications.cfg", config) .map_err(|err| Error::ConfigSerialization(err.into())) } pub fn write_private(config: &SectionConfigData) -> Result { - PRIVATE_CONFIG + private_config_parser() .write("priv/notifications.cfg", config) .map_err(|err| Error::ConfigSerialization(err.into())) } diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index f9917d9..c69ac26 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -13,12 +13,11 @@ use proxmox_section_config::SectionConfigData; use proxmox_uuid::Uuid; pub mod matcher; -use crate::config::CONFIG; use matcher::{MatcherConfig, MATCHER_TYPENAME}; pub mod api; -pub mod context; pub mod config; +pub mod context; pub mod endpoints; pub mod filter; pub mod group; @@ -280,7 +279,7 @@ impl Config { let default_config = context().default_config(); - let builtin_config = CONFIG + let builtin_config = config::config_parser() .parse("", default_config) .map_err(|err| Error::ConfigDeserialization(err.into()))?; -- 2.39.2 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel