From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id CAF561FF56B
	for <inbox@lore.proxmox.com>; Mon, 22 Apr 2024 09:51:02 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 48D5998A0;
	Mon, 22 Apr 2024 09:51:07 +0200 (CEST)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Mon, 22 Apr 2024 09:49:50 +0200
Message-Id: <20240422075030.73895-3-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240422075030.73895-1-l.wagner@proxmox.com>
References: <20240422075030.73895-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 v3 02/42] 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
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 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<SectionConfig> = 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<SectionConfig> = 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<String, Error> {
-    CONFIG
+    config_parser()
         .write("notifications.cfg", config)
         .map_err(|err| Error::ConfigSerialization(err.into()))
 }
 
 pub fn write_private(config: &SectionConfigData) -> Result<String, Error> {
-    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("<builtin>", 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