[pbs-devel] [PATCH proxmox v2 02/43] notify: use std::sync::OnceCell instead of lazy_static!

Lukas Wagner l.wagner at proxmox.com
Wed Apr 17 16:34:33 CEST 2024


Signed-off-by: Lukas Wagner <l.wagner at 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 68aa753..b5829fd 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;
@@ -279,7 +278,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





More information about the pbs-devel mailing list