[pbs-devel] [PATCH proxmox v2 05/12] rest-server: remove lazy_static dependency

Maximiliano Sandoval m.sandoval at proxmox.com
Wed Aug 14 09:19:54 CEST 2024


Signed-off-by: Maximiliano Sandoval <m.sandoval at proxmox.com>
---
 proxmox-rest-server/Cargo.toml                      |  2 +-
 proxmox-rest-server/examples/minimal-rest-server.rs |  8 +++-----
 proxmox-rest-server/src/lib.rs                      | 11 +++++++----
 proxmox-rest-server/src/rest.rs                     |  7 ++-----
 proxmox-rest-server/src/worker_task.rs              |  9 +++------
 5 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/proxmox-rest-server/Cargo.toml b/proxmox-rest-server/Cargo.toml
index 3f69e550..e35cb42f 100644
--- a/proxmox-rest-server/Cargo.toml
+++ b/proxmox-rest-server/Cargo.toml
@@ -6,6 +6,7 @@ edition.workspace = true
 license.workspace = true
 repository.workspace = true
 description = "REST server implementation"
+rust-version.workspace = true
 
 exclude.workspace = true
 
@@ -19,7 +20,6 @@ futures.workspace = true
 handlebars = { workspace = true, optional = true }
 http.workspace = true
 hyper = { workspace = true, features = [ "full" ] }
-lazy_static.workspace = true
 libc.workspace = true
 log.workspace = true
 nix.workspace = true
diff --git a/proxmox-rest-server/examples/minimal-rest-server.rs b/proxmox-rest-server/examples/minimal-rest-server.rs
index a93ef0f5..d6a5f2eb 100644
--- a/proxmox-rest-server/examples/minimal-rest-server.rs
+++ b/proxmox-rest-server/examples/minimal-rest-server.rs
@@ -1,13 +1,12 @@
 use std::collections::HashMap;
 use std::future::Future;
 use std::pin::Pin;
-use std::sync::Mutex;
+use std::sync::{LazyLock, Mutex};
 
 use anyhow::{bail, format_err, Error};
 use http::request::Parts;
 use http::HeaderMap;
 use hyper::{Body, Method, Response};
-use lazy_static::lazy_static;
 
 use proxmox_router::{
     list_subdirs_api_method, Router, RpcEnvironmentType, SubdirMap, UserInformation,
@@ -70,9 +69,8 @@ fn ping() -> Result<String, Error> {
     Ok("pong".to_string())
 }
 
-lazy_static! {
-    static ref ITEM_MAP: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
-}
+static ITEM_MAP: LazyLock<Mutex<HashMap<String, String>>> =
+    LazyLock::new(|| Mutex::new(HashMap::new()));
 
 #[api]
 /// Lists all current items
diff --git a/proxmox-rest-server/src/lib.rs b/proxmox-rest-server/src/lib.rs
index 3c9e887b..43dafa91 100644
--- a/proxmox-rest-server/src/lib.rs
+++ b/proxmox-rest-server/src/lib.rs
@@ -17,6 +17,7 @@
 #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
 
 use std::fmt;
+use std::sync::LazyLock;
 
 use anyhow::{format_err, Error};
 use nix::unistd::Pid;
@@ -46,10 +47,12 @@ pub use worker_task::*;
 mod h2service;
 pub use h2service::*;
 
-lazy_static::lazy_static! {
-    static ref PID: i32 = unsafe { libc::getpid() };
-    static ref PSTART: u64 = PidStat::read_from_pid(Pid::from_raw(*PID)).unwrap().starttime;
-}
+static PID: LazyLock<i32> = LazyLock::new(|| unsafe { libc::getpid() });
+static PSTART: LazyLock<u64> = LazyLock::new(|| {
+    PidStat::read_from_pid(Pid::from_raw(*PID))
+        .unwrap()
+        .starttime
+});
 
 /// Returns the current process ID (see [libc::getpid])
 ///
diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs
index fa9e043a..8b444c80 100644
--- a/proxmox-rest-server/src/rest.rs
+++ b/proxmox-rest-server/src/rest.rs
@@ -4,7 +4,7 @@ use std::hash::BuildHasher;
 use std::io;
 use std::path::{Path, PathBuf};
 use std::pin::Pin;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
 use std::task::{Context, Poll};
 
 use anyhow::{bail, format_err, Error};
@@ -14,7 +14,6 @@ use hyper::body::HttpBody;
 use hyper::header::{self, HeaderMap};
 use hyper::http::request::Parts;
 use hyper::{Body, Request, Response, StatusCode};
-use lazy_static::lazy_static;
 use regex::Regex;
 use serde_json::Value;
 use tokio::fs::File;
@@ -289,9 +288,7 @@ fn log_response(
 }
 
 fn get_proxied_peer(headers: &HeaderMap) -> Option<std::net::SocketAddr> {
-    lazy_static! {
-        static ref RE: Regex = Regex::new(r#"for="([^"]+)""#).unwrap();
-    }
+    static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"for="([^"]+)""#).unwrap());
     let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
     let capture = RE.captures(forwarded)?;
     let rhost = capture.get(1)?.as_str();
diff --git a/proxmox-rest-server/src/worker_task.rs b/proxmox-rest-server/src/worker_task.rs
index 62e5893f..228abb7f 100644
--- a/proxmox-rest-server/src/worker_task.rs
+++ b/proxmox-rest-server/src/worker_task.rs
@@ -4,12 +4,11 @@ use std::io::{BufRead, BufReader, Read, Write};
 use std::panic::UnwindSafe;
 use std::path::PathBuf;
 use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
-use std::sync::{Arc, Mutex, OnceLock};
+use std::sync::{Arc, LazyLock, Mutex, OnceLock};
 use std::time::{Duration, SystemTime};
 
 use anyhow::{bail, format_err, Error};
 use futures::*;
-use lazy_static::lazy_static;
 use nix::fcntl::OFlag;
 use once_cell::sync::OnceCell;
 use serde::{Deserialize, Serialize};
@@ -497,10 +496,8 @@ pub fn upid_read_status(upid: &UPID) -> Result<TaskState, Error> {
     Ok(TaskState::Unknown { endtime }) // no last line with both, end-time and task-state, found.
 }
 
-lazy_static! {
-    static ref WORKER_TASK_LIST: Mutex<HashMap<usize, Arc<WorkerTask>>> =
-        Mutex::new(HashMap::new());
-}
+static WORKER_TASK_LIST: LazyLock<Mutex<HashMap<usize, Arc<WorkerTask>>>> =
+    LazyLock::new(|| Mutex::new(HashMap::new()));
 
 /// checks if the task UPID refers to a worker from this process
 fn is_local_worker(upid: &UPID) -> bool {
-- 
2.39.2





More information about the pbs-devel mailing list