From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id BC8121FF19C for ; Wed, 14 Aug 2024 09:19:59 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6EAFDFCE5; Wed, 14 Aug 2024 09:20:11 +0200 (CEST) From: Maximiliano Sandoval To: pbs-devel@lists.proxmox.com Date: Wed, 14 Aug 2024 09:19:54 +0200 Message-Id: <20240814072001.53422-5-m.sandoval@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814072001.53422-1-m.sandoval@proxmox.com> References: <20240814072001.53422-1-m.sandoval@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.111 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [PATCH proxmox v2 05/12] rest-server: remove lazy_static dependency 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: Maximiliano Sandoval --- 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 { Ok("pong".to_string()) } -lazy_static! { - static ref ITEM_MAP: Mutex> = Mutex::new(HashMap::new()); -} +static ITEM_MAP: LazyLock>> = + 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 = LazyLock::new(|| unsafe { libc::getpid() }); +static PSTART: LazyLock = 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 { - lazy_static! { - static ref RE: Regex = Regex::new(r#"for="([^"]+)""#).unwrap(); - } + static RE: LazyLock = 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 { Ok(TaskState::Unknown { endtime }) // no last line with both, end-time and task-state, found. } -lazy_static! { - static ref WORKER_TASK_LIST: Mutex>> = - Mutex::new(HashMap::new()); -} +static WORKER_TASK_LIST: LazyLock>>> = + 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 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel