From 0294bde56007ddb898601cccf3b547238547e62f Mon Sep 17 00:00:00 2001 From: Luca Giannini <68999840+structwafel@users.noreply.github.com> Date: Thu, 5 Sep 2024 10:04:12 +0200 Subject: [PATCH] Refactor migrating away from once cell (#4614) Co-authored-by: Tobie Morgan Hitchcock --- Cargo.lock | 4 --- Cargo.toml | 1 - core/Cargo.toml | 3 +- core/src/cnf/mod.rs | 33 ++++++++++---------- core/src/exe/spawn.rs | 4 +-- core/src/fnc/string.rs | 6 ++-- core/src/fnc/util/string/fuzzy.rs | 4 +-- core/src/fnc/util/string/slug.rs | 6 ++-- core/src/iam/entities/schema.rs | 4 +-- core/src/iam/jwks.rs | 20 ++++++------- core/src/iam/policies/policy_set.rs | 4 +-- core/src/iam/token.rs | 4 +-- core/src/iam/verify.rs | 6 ++-- core/src/kvs/fdb/cnf.rs | 8 ++--- core/src/kvs/fdb/mod.rs | 6 ++-- core/src/kvs/rocksdb/cnf.rs | 20 ++++++------- core/src/mac/mod.rs | 6 ++-- core/src/obs/mod.rs | 10 +++---- core/src/rpc/request.rs | 8 ++--- core/src/sql/paths.rs | 26 ++++++++-------- core/src/sql/regex.rs | 4 +-- docker/Dockerfile | 2 +- sdk/Cargo.toml | 3 +- sdk/benches/sdb_benches/mod.rs | 20 +++++++------ sdk/benches/sdb_benches/sdk/mod.rs | 4 +-- sdk/examples/actix/Cargo.toml | 1 - sdk/examples/actix/src/main.rs | 4 +-- sdk/examples/concurrency/main.rs | 4 +-- sdk/fuzz/Cargo.lock | 14 --------- sdk/src/api/engine/any/mod.rs | 4 +-- sdk/src/api/engine/local/mod.rs | 4 +-- sdk/src/api/engine/remote/http/mod.rs | 4 +-- sdk/src/api/engine/remote/ws/mod.rs | 4 +-- sdk/src/api/method/mod.rs | 8 ++--- sdk/src/api/method/tests/mod.rs | 4 +-- sdk/tests/api.rs | 2 +- src/cnf/mod.rs | 43 ++++++++++++++------------- src/env/mod.rs | 6 ++-- src/telemetry/metrics/http/mod.rs | 12 ++++---- src/telemetry/metrics/ws/mod.rs | 12 ++++---- src/telemetry/mod.rs | 4 +-- 41 files changed, 164 insertions(+), 182 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 215d046b..edbb370f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,7 +34,6 @@ name = "actix-example" version = "0.1.0" dependencies = [ "actix-web", - "once_cell", "serde", "surrealdb", "thiserror", @@ -5948,7 +5947,6 @@ dependencies = [ "jsonwebtoken", "mimalloc", "nix 0.27.1", - "once_cell", "opentelemetry", "opentelemetry-otlp", "opentelemetry-proto", @@ -6043,7 +6041,6 @@ dependencies = [ "hashbrown 0.14.5", "indexmap 2.2.6", "native-tls", - "once_cell", "path-clean", "pharos", "pprof", @@ -6131,7 +6128,6 @@ dependencies = [ "num-traits", "num_cpus", "object_store", - "once_cell", "pbkdf2", "pharos", "phf", diff --git a/Cargo.toml b/Cargo.toml index a3afed0c..0351b548 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,6 @@ http = "1.1.0" http-body = "1.0.0" http-body-util = "0.1.1" hyper = "1.4.1" -once_cell = "1.18.0" opentelemetry = { version = "0.24" } opentelemetry_sdk = { version = "0.24", features = ["rt-tokio"] } opentelemetry-otlp = { version = "0.17.0", features = ["metrics"] } diff --git a/core/Cargo.toml b/core/Cargo.toml index 4b1a0131..0b88376f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -3,7 +3,7 @@ name = "surrealdb-core" publish = true edition = "2021" version = "2.0.0" -rust-version = "1.80.0" +rust-version = "1.80.1" readme = "../sdk/CARGO.md" authors = ["Tobie Morgan Hitchcock "] description = "A scalable, distributed, collaborative, document-graph database, for the realtime web" @@ -108,7 +108,6 @@ ndarray-stats = "=0.5.1" num_cpus = "1.16.0" num-traits = "0.2.18" object_store = { version = "0.10.2", optional = false } -once_cell = "1.18.0" pbkdf2 = { version = "0.12.2", features = ["simple"] } phf = { version = "0.11.2", features = ["macros", "unicase"] } pin-project-lite = "0.2.13" diff --git a/core/src/cnf/mod.rs b/core/src/cnf/mod.rs index f4215422..b875e6e0 100644 --- a/core/src/cnf/mod.rs +++ b/core/src/cnf/mod.rs @@ -1,4 +1,4 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; /// The characters which are supported in server record IDs. pub const ID_CHARS: [char; 36] = [ @@ -14,41 +14,42 @@ pub const PROTECTED_PARAM_NAMES: &[&str] = &["access", "auth", "token", "session /// Specifies how many concurrent jobs can be buffered in the worker channel. #[cfg(not(target_arch = "wasm32"))] -pub static MAX_CONCURRENT_TASKS: Lazy = +pub static MAX_CONCURRENT_TASKS: LazyLock = lazy_env_parse!("SURREAL_MAX_CONCURRENT_TASKS", usize, 64); /// Specifies how deep computation recursive call will go before en error is returned. -pub static MAX_COMPUTATION_DEPTH: Lazy = +pub static MAX_COMPUTATION_DEPTH: LazyLock = lazy_env_parse!("SURREAL_MAX_COMPUTATION_DEPTH", u32, 120); /// Specifies the number of items which can be cached within a single transaction. -pub static TRANSACTION_CACHE_SIZE: Lazy = +pub static TRANSACTION_CACHE_SIZE: LazyLock = lazy_env_parse!("SURREAL_TRANSACTION_CACHE_SIZE", usize, 10_000); /// The maximum number of keys that should be scanned at once in general queries. -pub static NORMAL_FETCH_SIZE: Lazy = lazy_env_parse!("SURREAL_NORMAL_FETCH_SIZE", u32, 50); +pub static NORMAL_FETCH_SIZE: LazyLock = lazy_env_parse!("SURREAL_NORMAL_FETCH_SIZE", u32, 50); /// The maximum number of keys that should be scanned at once for export queries. -pub static EXPORT_BATCH_SIZE: Lazy = lazy_env_parse!("SURREAL_EXPORT_BATCH_SIZE", u32, 1000); +pub static EXPORT_BATCH_SIZE: LazyLock = + lazy_env_parse!("SURREAL_EXPORT_BATCH_SIZE", u32, 1000); /// The maximum number of keys that should be fetched when streaming range scans in a Scanner. -pub static MAX_STREAM_BATCH_SIZE: Lazy = +pub static MAX_STREAM_BATCH_SIZE: LazyLock = lazy_env_parse!("SURREAL_MAX_STREAM_BATCH_SIZE", u32, 1000); /// The maximum number of keys that should be scanned at once per concurrent indexing batch. -pub static INDEXING_BATCH_SIZE: Lazy = +pub static INDEXING_BATCH_SIZE: LazyLock = lazy_env_parse!("SURREAL_INDEXING_BATCH_SIZE", u32, 250); /// The maximum stack size of the JavaScript function runtime (defaults to 256 KiB) -pub static SCRIPTING_MAX_STACK_SIZE: Lazy = +pub static SCRIPTING_MAX_STACK_SIZE: LazyLock = lazy_env_parse!("SURREAL_SCRIPTING_MAX_STACK_SIZE", usize, 256 * 1024); /// The maximum memory limit of the JavaScript function runtime (defaults to 2 MiB). -pub static SCRIPTING_MAX_MEMORY_LIMIT: Lazy = +pub static SCRIPTING_MAX_MEMORY_LIMIT: LazyLock = lazy_env_parse!("SURREAL_SCRIPTING_MAX_MEMORY_LIMIT", usize, 2 << 20); /// Forward all signup/signin/authenticate query errors to a client performing authentication. Do not use in production. -pub static INSECURE_FORWARD_ACCESS_ERRORS: Lazy = +pub static INSECURE_FORWARD_ACCESS_ERRORS: LazyLock = lazy_env_parse!("SURREAL_INSECURE_FORWARD_ACCESS_ERRORS", bool, false); #[cfg(any( @@ -60,25 +61,25 @@ pub static INSECURE_FORWARD_ACCESS_ERRORS: Lazy = feature = "kv-surrealcs", ))] /// Specifies the buffer limit for external sorting. -pub static EXTERNAL_SORTING_BUFFER_LIMIT: Lazy = +pub static EXTERNAL_SORTING_BUFFER_LIMIT: LazyLock = lazy_env_parse!("SURREAL_EXTERNAL_SORTING_BUFFER_LIMIT", usize, 50_000); /// Specifies whether GraphQL querying and schema definition is enabled. -pub static GRAPHQL_ENABLE: Lazy = +pub static GRAPHQL_ENABLE: LazyLock = lazy_env_parse!("SURREAL_EXPERIMENTAL_GRAPHQL", bool, false); /// Enable experimental bearer access and stateful access grant management. Still under active development. /// Using this experimental feature may introduce risks related to breaking changes and security issues. #[cfg(not(test))] -pub static EXPERIMENTAL_BEARER_ACCESS: Lazy = +pub static EXPERIMENTAL_BEARER_ACCESS: LazyLock = lazy_env_parse!("SURREAL_EXPERIMENTAL_BEARER_ACCESS", bool, false); /// Run tests with bearer access enabled as it introduces new functionality that needs to be tested. #[cfg(test)] -pub static EXPERIMENTAL_BEARER_ACCESS: Lazy = Lazy::new(|| true); +pub static EXPERIMENTAL_BEARER_ACCESS: LazyLock = LazyLock::new(|| true); /// Used to limit allocation for builtin functions -pub static GENERATION_ALLOCATION_LIMIT: Lazy = once_cell::sync::Lazy::new(|| { +pub static GENERATION_ALLOCATION_LIMIT: LazyLock = LazyLock::new(|| { let n = std::env::var("SURREAL_GENERATION_ALLOCATION_LIMIT") .map(|s| s.parse::().unwrap_or(20)) .unwrap_or(20); diff --git a/core/src/exe/spawn.rs b/core/src/exe/spawn.rs index 3b5e19a1..3da31afe 100644 --- a/core/src/exe/spawn.rs +++ b/core/src/exe/spawn.rs @@ -1,12 +1,12 @@ #![cfg(not(target_arch = "wasm32"))] use executor::{Executor, Task}; -use once_cell::sync::Lazy; use std::future::Future; use std::panic::catch_unwind; +use std::sync::LazyLock; pub fn spawn(future: impl Future + Send + 'static) -> Task { - static GLOBAL: Lazy> = Lazy::new(|| { + static GLOBAL: LazyLock> = LazyLock::new(|| { std::thread::spawn(|| { catch_unwind(|| { futures::executor::block_on(GLOBAL.run(futures::future::pending::<()>())) diff --git a/core/src/fnc/string.rs b/core/src/fnc/string.rs index be870b7d..5da615bb 100644 --- a/core/src/fnc/string.rs +++ b/core/src/fnc/string.rs @@ -186,16 +186,16 @@ pub mod is { use crate::sql::value::Value; use crate::sql::{Datetime, Thing}; use chrono::NaiveDateTime; - use once_cell::sync::Lazy; use regex::Regex; use semver::Version; use std::char; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + use std::sync::LazyLock; use url::Url; use uuid::Uuid; - #[rustfmt::skip] static LATITUDE_RE: Lazy = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap()); - #[rustfmt::skip] static LONGITUDE_RE: Lazy = Lazy::new(|| Regex::new("^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$").unwrap()); + #[rustfmt::skip] static LATITUDE_RE: LazyLock = LazyLock::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap()); + #[rustfmt::skip] static LONGITUDE_RE: LazyLock = LazyLock::new(|| Regex::new("^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$").unwrap()); pub fn alphanum((arg,): (String,)) -> Result { Ok(arg.chars().all(char::is_alphanumeric).into()) diff --git a/core/src/fnc/util/string/fuzzy.rs b/core/src/fnc/util/string/fuzzy.rs index f2044745..c9a9d7d3 100644 --- a/core/src/fnc/util/string/fuzzy.rs +++ b/core/src/fnc/util/string/fuzzy.rs @@ -1,8 +1,8 @@ use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::FuzzyMatcher; -use once_cell::sync::Lazy; +use std::sync::LazyLock; -static MATCHER: Lazy = Lazy::new(|| SkimMatcherV2::default().ignore_case()); +static MATCHER: LazyLock = LazyLock::new(|| SkimMatcherV2::default().ignore_case()); pub trait Fuzzy { /// Retrieve the fuzzy similarity score of this &str compared to another &str diff --git a/core/src/fnc/util/string/slug.rs b/core/src/fnc/util/string/slug.rs index cee1ce4d..19eda6a6 100644 --- a/core/src/fnc/util/string/slug.rs +++ b/core/src/fnc/util/string/slug.rs @@ -1,9 +1,9 @@ use ascii::any_ascii as ascii; -use once_cell::sync::Lazy; use regex::Regex; +use std::sync::LazyLock; -static SIMPLES: Lazy = Lazy::new(|| Regex::new("[^a-z0-9-_]").unwrap()); -static HYPHENS: Lazy = Lazy::new(|| Regex::new("-+").unwrap()); +static SIMPLES: LazyLock = LazyLock::new(|| Regex::new("[^a-z0-9-_]").unwrap()); +static HYPHENS: LazyLock = LazyLock::new(|| Regex::new("-+").unwrap()); pub fn slug>(s: S) -> String { // Get a reference diff --git a/core/src/iam/entities/schema.rs b/core/src/iam/entities/schema.rs index db3c35aa..36568347 100644 --- a/core/src/iam/entities/schema.rs +++ b/core/src/iam/entities/schema.rs @@ -1,7 +1,7 @@ use cedar_policy::Schema; -use once_cell::sync::Lazy; +use std::sync::LazyLock; -pub static DEFAULT_CEDAR_SCHEMA: Lazy = Lazy::new(|| { +pub static DEFAULT_CEDAR_SCHEMA: LazyLock = LazyLock::new(|| { serde_json::json!( { "": { diff --git a/core/src/iam/jwks.rs b/core/src/iam/jwks.rs index 09d41014..6e410692 100644 --- a/core/src/iam/jwks.rs +++ b/core/src/iam/jwks.rs @@ -6,13 +6,13 @@ use jsonwebtoken::jwk::{ AlgorithmParameters::*, Jwk, JwkSet, KeyAlgorithm, KeyOperations, PublicKeyUse, }; use jsonwebtoken::{Algorithm::*, DecodingKey, Validation}; -use once_cell::sync::Lazy; use reqwest::{Client, Url}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::collections::HashMap; use std::str::FromStr; use std::sync::Arc; +use std::sync::LazyLock; use tokio::sync::RwLock; pub(crate) type JwksCache = HashMap; @@ -23,10 +23,10 @@ pub(crate) struct JwksCacheEntry { } #[cfg(test)] -static CACHE_EXPIRATION: Lazy = Lazy::new(|| Duration::seconds(1)); +static CACHE_EXPIRATION: LazyLock = LazyLock::new(|| Duration::seconds(1)); #[cfg(not(test))] -static CACHE_EXPIRATION: Lazy = - Lazy::new(|| match std::env::var("SURREAL_JWKS_CACHE_EXPIRATION_SECONDS") { +static CACHE_EXPIRATION: LazyLock = + LazyLock::new(|| match std::env::var("SURREAL_JWKS_CACHE_EXPIRATION_SECONDS") { Ok(seconds_str) => { let seconds = seconds_str.parse::().expect( "Expected a valid number of seconds for SURREAL_JWKS_CACHE_EXPIRATION_SECONDS", @@ -39,10 +39,10 @@ static CACHE_EXPIRATION: Lazy = }); #[cfg(test)] -static CACHE_COOLDOWN: Lazy = Lazy::new(|| Duration::seconds(300)); +static CACHE_COOLDOWN: LazyLock = LazyLock::new(|| Duration::seconds(300)); #[cfg(not(test))] -static CACHE_COOLDOWN: Lazy = - Lazy::new(|| match std::env::var("SURREAL_JWKS_CACHE_COOLDOWN_SECONDS") { +static CACHE_COOLDOWN: LazyLock = + LazyLock::new(|| match std::env::var("SURREAL_JWKS_CACHE_COOLDOWN_SECONDS") { Ok(seconds_str) => { let seconds = seconds_str.parse::().expect( "Expected a valid number of seconds for SURREAL_JWKS_CACHE_COOLDOWN_SECONDS", @@ -55,8 +55,8 @@ static CACHE_COOLDOWN: Lazy = }); #[cfg(not(target_arch = "wasm32"))] -static REMOTE_TIMEOUT: Lazy = - Lazy::new(|| match std::env::var("SURREAL_JWKS_REMOTE_TIMEOUT_MILLISECONDS") { +static REMOTE_TIMEOUT: LazyLock = + LazyLock::new(|| match std::env::var("SURREAL_JWKS_REMOTE_TIMEOUT_MILLISECONDS") { Ok(milliseconds_str) => { let milliseconds = milliseconds_str .parse::() @@ -365,7 +365,7 @@ mod tests { rng.sample_iter(&Alphanumeric).take(8).map(char::from).collect() } - static DEFAULT_JWKS: Lazy = Lazy::new(|| { + static DEFAULT_JWKS: LazyLock = LazyLock::new(|| { JwkSet{ keys: vec![Jwk{ common: jsonwebtoken::jwk::CommonParameters { diff --git a/core/src/iam/policies/policy_set.rs b/core/src/iam/policies/policy_set.rs index dc658990..3a2a1efd 100644 --- a/core/src/iam/policies/policy_set.rs +++ b/core/src/iam/policies/policy_set.rs @@ -1,9 +1,9 @@ use std::str::FromStr; use cedar_policy::PolicySet; -use once_cell::sync::Lazy; +use std::sync::LazyLock; -pub static POLICY_SET: Lazy = Lazy::new(|| { +pub static POLICY_SET: LazyLock = LazyLock::new(|| { PolicySet::from_str( r#" // All roles can view all resources on the same level hierarchy or below diff --git a/core/src/iam/token.rs b/core/src/iam/token.rs index 37875d41..e7dcd44e 100644 --- a/core/src/iam/token.rs +++ b/core/src/iam/token.rs @@ -2,11 +2,11 @@ use crate::sql::json; use crate::sql::Object; use crate::sql::Value; use jsonwebtoken::{Algorithm, Header}; -use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use std::sync::LazyLock; -pub static HEADER: Lazy
= Lazy::new(|| Header::new(Algorithm::HS512)); +pub static HEADER: LazyLock
= LazyLock::new(|| Header::new(Algorithm::HS512)); #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(untagged)] diff --git a/core/src/iam/verify.rs b/core/src/iam/verify.rs index d41381df..57c96f2d 100644 --- a/core/src/iam/verify.rs +++ b/core/src/iam/verify.rs @@ -11,9 +11,9 @@ use crate::syn; use argon2::{Argon2, PasswordHash, PasswordVerifier}; use chrono::Utc; use jsonwebtoken::{decode, DecodingKey, Validation}; -use once_cell::sync::Lazy; use std::str::{self, FromStr}; use std::sync::Arc; +use std::sync::LazyLock; fn config(alg: Algorithm, key: &[u8]) -> Result<(DecodingKey, Validation), Error> { let (dec, mut val) = match alg { @@ -67,9 +67,9 @@ fn config(alg: Algorithm, key: &[u8]) -> Result<(DecodingKey, Validation), Error Ok((dec, val)) } -static KEY: Lazy = Lazy::new(|| DecodingKey::from_secret(&[])); +static KEY: LazyLock = LazyLock::new(|| DecodingKey::from_secret(&[])); -static DUD: Lazy = Lazy::new(|| { +static DUD: LazyLock = LazyLock::new(|| { let mut validation = Validation::new(jsonwebtoken::Algorithm::HS256); validation.insecure_disable_signature_validation(); validation.validate_nbf = false; diff --git a/core/src/kvs/fdb/cnf.rs b/core/src/kvs/fdb/cnf.rs index e076b40c..d2cd055f 100644 --- a/core/src/kvs/fdb/cnf.rs +++ b/core/src/kvs/fdb/cnf.rs @@ -1,10 +1,10 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; -pub static FOUNDATIONDB_TRANSACTION_TIMEOUT: Lazy = +pub static FOUNDATIONDB_TRANSACTION_TIMEOUT: LazyLock = lazy_env_parse_or_else!("SURREAL_FOUNDATIONDB_TRANSACTION_TIMEOUT", i32, |_| { 5000 }); -pub static FOUNDATIONDB_TRANSACTION_RETRY_LIMIT: Lazy = +pub static FOUNDATIONDB_TRANSACTION_RETRY_LIMIT: LazyLock = lazy_env_parse_or_else!("SURREAL_FOUNDATIONDB_TRANSACTION_RETRY_LIMIT", i32, |_| { 5 }); -pub static FOUNDATIONDB_TRANSACTION_MAX_RETRY_DELAY: Lazy = +pub static FOUNDATIONDB_TRANSACTION_MAX_RETRY_DELAY: LazyLock = lazy_env_parse_or_else!("SURREAL_FOUNDATIONDB_TRANSACTION_MAX_RETRY_DELAY", i32, |_| { 500 }); diff --git a/core/src/kvs/fdb/mod.rs b/core/src/kvs/fdb/mod.rs index 0ce09f40..da7c4661 100644 --- a/core/src/kvs/fdb/mod.rs +++ b/core/src/kvs/fdb/mod.rs @@ -14,10 +14,10 @@ use foundationdb::Database; use foundationdb::RangeOption; use foundationdb::Transaction as Tx; use futures::StreamExt; -use once_cell::sync::Lazy; use std::fmt::Debug; use std::ops::Range; use std::sync::Arc; +use std::sync::LazyLock; const TIMESTAMP: [u8; 10] = [0x00; 10]; @@ -91,8 +91,8 @@ impl Datastore { /// for more information on cluster connection files. pub(crate) async fn new(path: &str) -> Result { // Initialize the FoundationDB Client API - static FDBNET: Lazy> = - Lazy::new(|| Arc::new(unsafe { foundationdb::boot() })); + static FDBNET: LazyLock> = + LazyLock::new(|| Arc::new(unsafe { foundationdb::boot() })); // Store the network cancellation handle let _fdbnet = (*FDBNET).clone(); // Configure and setup the database diff --git a/core/src/kvs/rocksdb/cnf.rs b/core/src/kvs/rocksdb/cnf.rs index ba5751ae..37f2c3bf 100644 --- a/core/src/kvs/rocksdb/cnf.rs +++ b/core/src/kvs/rocksdb/cnf.rs @@ -1,28 +1,28 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; -pub static ROCKSDB_THREAD_COUNT: Lazy = +pub static ROCKSDB_THREAD_COUNT: LazyLock = lazy_env_parse_or_else!("SURREAL_ROCKSDB_THREAD_COUNT", i32, |_| num_cpus::get() as i32); -pub static ROCKSDB_WRITE_BUFFER_SIZE: Lazy = +pub static ROCKSDB_WRITE_BUFFER_SIZE: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_WRITE_BUFFER_SIZE", usize, 256 * 1024 * 1024); -pub static ROCKSDB_TARGET_FILE_SIZE_BASE: Lazy = +pub static ROCKSDB_TARGET_FILE_SIZE_BASE: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_TARGET_FILE_SIZE_BASE", u64, 512 * 1024 * 1024); -pub static ROCKSDB_MAX_WRITE_BUFFER_NUMBER: Lazy = +pub static ROCKSDB_MAX_WRITE_BUFFER_NUMBER: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_MAX_WRITE_BUFFER_NUMBER", i32, 32); -pub static ROCKSDB_MIN_WRITE_BUFFER_NUMBER_TO_MERGE: Lazy = +pub static ROCKSDB_MIN_WRITE_BUFFER_NUMBER_TO_MERGE: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_MIN_WRITE_BUFFER_NUMBER_TO_MERGE", i32, 4); -pub static ROCKSDB_ENABLE_PIPELINED_WRITES: Lazy = +pub static ROCKSDB_ENABLE_PIPELINED_WRITES: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_ENABLE_PIPELINED_WRITES", bool, true); -pub static ROCKSDB_ENABLE_BLOB_FILES: Lazy = +pub static ROCKSDB_ENABLE_BLOB_FILES: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_ENABLE_BLOB_FILES", bool, true); -pub static ROCKSDB_MIN_BLOB_SIZE: Lazy = +pub static ROCKSDB_MIN_BLOB_SIZE: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_MIN_BLOB_SIZE", u64, 4 * 1024); -pub static ROCKSDB_KEEP_LOG_FILE_NUM: Lazy = +pub static ROCKSDB_KEEP_LOG_FILE_NUM: LazyLock = lazy_env_parse!("SURREAL_ROCKSDB_KEEP_LOG_FILE_NUM", usize, 20); diff --git a/core/src/mac/mod.rs b/core/src/mac/mod.rs index f2c6921e..0eb6012c 100644 --- a/core/src/mac/mod.rs +++ b/core/src/mac/mod.rs @@ -83,12 +83,12 @@ macro_rules! run { /// /// # Return Value /// -/// A lazy static variable of type `once_cell::sync::Lazy`, which holds the parsed value +/// A lazy static variable of type `std::sync::LazyLock`, which holds the parsed value /// from the environment variable or the default value. #[macro_export] macro_rules! lazy_env_parse { ($key:expr, $t:ty, $default:expr) => { - once_cell::sync::Lazy::new(|| { + std::sync::LazyLock::new(|| { std::env::var($key) .and_then(|s| Ok(s.parse::<$t>().unwrap_or($default))) .unwrap_or($default) @@ -111,7 +111,7 @@ macro_rules! lazy_env_parse { #[macro_export] macro_rules! lazy_env_parse_or_else { ($key:expr, $t:ty, $default:expr) => { - once_cell::sync::Lazy::new(|| { + std::sync::LazyLock::new(|| { std::env::var($key) .and_then(|s| Ok(s.parse::<$t>().unwrap_or_else($default))) .unwrap_or_else($default) diff --git a/core/src/obs/mod.rs b/core/src/obs/mod.rs index bdd8071b..ddeb457b 100644 --- a/core/src/obs/mod.rs +++ b/core/src/obs/mod.rs @@ -10,11 +10,11 @@ use object_store::memory::InMemory; use object_store::parse_url; use object_store::path::Path; use object_store::ObjectStore; -use once_cell::sync::Lazy; use sha1::{Digest, Sha1}; use std::env; use std::fs; use std::sync::Arc; +use std::sync::LazyLock; use url::Url; fn initialize_store(env_var: &str, default_dir: &str) -> Arc { @@ -46,11 +46,11 @@ fn initialize_store(env_var: &str, default_dir: &str) -> Arc { } } -static STORE: Lazy> = - Lazy::new(|| initialize_store("SURREAL_OBJECT_STORE", "store")); +static STORE: LazyLock> = + LazyLock::new(|| initialize_store("SURREAL_OBJECT_STORE", "store")); -static CACHE: Lazy> = - Lazy::new(|| initialize_store("SURREAL_CACHE_STORE", "cache")); +static CACHE: LazyLock> = + LazyLock::new(|| initialize_store("SURREAL_CACHE_STORE", "cache")); /// Streams the file from the local system or memory object storage. pub async fn stream( diff --git a/core/src/rpc/request.rs b/core/src/rpc/request.rs index 22ff8180..a57259e4 100644 --- a/core/src/rpc/request.rs +++ b/core/src/rpc/request.rs @@ -3,11 +3,11 @@ use crate::rpc::format::msgpack::Pack; use crate::rpc::RpcError; use crate::sql::Part; use crate::sql::{Array, Value}; -use once_cell::sync::Lazy; +use std::sync::LazyLock; -pub static ID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); -pub static METHOD: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("method")]); -pub static PARAMS: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("params")]); +pub static ID: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("id")]); +pub static METHOD: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("method")]); +pub static PARAMS: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("params")]); #[derive(Debug)] pub struct Request { diff --git a/core/src/sql/paths.rs b/core/src/sql/paths.rs index f3262916..a0b656cc 100644 --- a/core/src/sql/paths.rs +++ b/core/src/sql/paths.rs @@ -1,30 +1,30 @@ use crate::sql::part::Part; -use once_cell::sync::Lazy; +use std::sync::LazyLock; pub const OBJ_PATH_ACCESS: &str = "ac"; pub const OBJ_PATH_AUTH: &str = "rd"; pub const OBJ_PATH_TOKEN: &str = "tk"; -pub static ID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); +pub static ID: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("id")]); -pub static IP: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("ip")]); +pub static IP: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("ip")]); -pub static NS: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("ns")]); +pub static NS: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("ns")]); -pub static DB: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("db")]); +pub static DB: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("db")]); -pub static AC: Lazy<[Part; 1]> = Lazy::new(|| [Part::from(OBJ_PATH_ACCESS)]); +pub static AC: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from(OBJ_PATH_ACCESS)]); -pub static RD: Lazy<[Part; 1]> = Lazy::new(|| [Part::from(OBJ_PATH_AUTH)]); +pub static RD: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from(OBJ_PATH_AUTH)]); -pub static OR: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("or")]); +pub static OR: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("or")]); -pub static TK: Lazy<[Part; 1]> = Lazy::new(|| [Part::from(OBJ_PATH_TOKEN)]); +pub static TK: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from(OBJ_PATH_TOKEN)]); -pub static IN: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("in")]); +pub static IN: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("in")]); -pub static OUT: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("out")]); +pub static OUT: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("out")]); -pub static META: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("__")]); +pub static META: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("__")]); -pub static EDGE: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("__")]); +pub static EDGE: LazyLock<[Part; 1]> = LazyLock::new(|| [Part::from("__")]); diff --git a/core/src/sql/regex.rs b/core/src/sql/regex.rs index 60163b3b..81bcf648 100644 --- a/core/src/sql/regex.rs +++ b/core/src/sql/regex.rs @@ -1,4 +1,3 @@ -use once_cell::sync::Lazy; use quick_cache::sync::{Cache, GuardResult}; use revision::revisioned; use serde::{ @@ -10,6 +9,7 @@ use std::fmt::Debug; use std::fmt::{self, Display, Formatter}; use std::hash::{Hash, Hasher}; use std::str::FromStr; +use std::sync::LazyLock; use std::{env, str}; pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Regex"; @@ -27,7 +27,7 @@ impl Regex { } fn regex_new(str: &str) -> Result { - static REGEX_CACHE: Lazy> = Lazy::new(|| { + static REGEX_CACHE: LazyLock> = LazyLock::new(|| { let cache_size: usize = env::var("SURREAL_REGEX_CACHE_SIZE") .map_or(1000, |v| v.parse().unwrap_or(1000)) .max(10); // The minimum cache size is 10 diff --git a/docker/Dockerfile b/docker/Dockerfile index ac71355a..30ddbc00 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -9,7 +9,7 @@ FROM docker.io/rockylinux:8 as builder RUN yum install -y gcc-toolset-13 git cmake llvm-toolset patch zlib-devel python3.11 # Install rust -ARG RUST_VERSION=1.80.0 +ARG RUST_VERSION=1.80.1 RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > /tmp/rustup.sh RUN bash /tmp/rustup.sh -y --default-toolchain ${RUST_VERSION} ENV PATH="/root/.cargo/bin:${PATH}" diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 2b1b4864..17fd6d21 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -3,7 +3,7 @@ name = "surrealdb" publish = true edition = "2021" version = "2.0.0" -rust-version = "1.80.0" +rust-version = "1.80.1" readme = "CARGO.md" authors = ["Tobie Morgan Hitchcock "] description = "A scalable, distributed, collaborative, document-graph database, for the realtime web" @@ -75,7 +75,6 @@ futures = "0.3.30" geo = { version = "0.28.0", features = ["use-serde"] } indexmap = { version = "2.1.0", features = ["serde"] } native-tls = { version = "0.2.11", optional = true } -once_cell = "1.18.0" path-clean = "1.0.1" reqwest = { version = "0.12.7", default-features = false, features = [ "json", diff --git a/sdk/benches/sdb_benches/mod.rs b/sdk/benches/sdb_benches/mod.rs index 5b9dd5e4..adff12ad 100644 --- a/sdk/benches/sdb_benches/mod.rs +++ b/sdk/benches/sdb_benches/mod.rs @@ -1,19 +1,21 @@ use criterion::Criterion; -use once_cell::sync::Lazy; +use std::sync::LazyLock; use std::sync::OnceLock; use tokio::runtime::Runtime; mod lib; mod sdk; -static NUM_OPS: Lazy = - Lazy::new(|| std::env::var("BENCH_NUM_OPS").unwrap_or("1000".to_string()).parse().unwrap()); -static DURATION_SECS: Lazy = - Lazy::new(|| std::env::var("BENCH_DURATION").unwrap_or("30".to_string()).parse().unwrap()); -static SAMPLE_SIZE: Lazy = - Lazy::new(|| std::env::var("BENCH_SAMPLE_SIZE").unwrap_or("30".to_string()).parse().unwrap()); -static WORKER_THREADS: Lazy = - Lazy::new(|| std::env::var("BENCH_WORKER_THREADS").unwrap_or("1".to_string()).parse().unwrap()); +static NUM_OPS: LazyLock = + LazyLock::new(|| std::env::var("BENCH_NUM_OPS").unwrap_or("1000".to_string()).parse().unwrap()); +static DURATION_SECS: LazyLock = + LazyLock::new(|| std::env::var("BENCH_DURATION").unwrap_or("30".to_string()).parse().unwrap()); +static SAMPLE_SIZE: LazyLock = LazyLock::new(|| { + std::env::var("BENCH_SAMPLE_SIZE").unwrap_or("30".to_string()).parse().unwrap() +}); +static WORKER_THREADS: LazyLock = LazyLock::new(|| { + std::env::var("BENCH_WORKER_THREADS").unwrap_or("1".to_string()).parse().unwrap() +}); static RUNTIME: OnceLock = OnceLock::new(); fn rt() -> &'static Runtime { diff --git a/sdk/benches/sdb_benches/sdk/mod.rs b/sdk/benches/sdb_benches/sdk/mod.rs index a15dbf23..a207ff94 100644 --- a/sdk/benches/sdb_benches/sdk/mod.rs +++ b/sdk/benches/sdb_benches/sdk/mod.rs @@ -1,12 +1,12 @@ use criterion::{Criterion, Throughput}; -use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; +use std::sync::LazyLock; use std::time::Duration; use surrealdb::{engine::any::Any, sql::Id, Surreal}; mod routines; -static DB: Lazy> = Lazy::new(Surreal::init); +static DB: LazyLock> = LazyLock::new(Surreal::init); #[derive(Debug, Clone, Serialize, Deserialize)] struct Record { diff --git a/sdk/examples/actix/Cargo.toml b/sdk/examples/actix/Cargo.toml index 2372c9b8..c523a499 100644 --- a/sdk/examples/actix/Cargo.toml +++ b/sdk/examples/actix/Cargo.toml @@ -6,7 +6,6 @@ publish = false [dependencies] actix-web = { version = "4.4.0", features = ["macros"] } -once_cell = "1.18.0" serde = { version = "1.0.209", features = ["derive"] } surrealdb = { path = "../.." } thiserror = "1.0.63" diff --git a/sdk/examples/actix/src/main.rs b/sdk/examples/actix/src/main.rs index c224c524..a2372b65 100644 --- a/sdk/examples/actix/src/main.rs +++ b/sdk/examples/actix/src/main.rs @@ -2,13 +2,13 @@ mod error; mod person; use actix_web::{App, HttpServer}; -use once_cell::sync::Lazy; +use std::sync::LazyLock; use surrealdb::engine::remote::ws::Client; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; use surrealdb::Surreal; -static DB: Lazy> = Lazy::new(Surreal::init); +static DB: LazyLock> = LazyLock::new(Surreal::init); #[actix_web::main] async fn main() -> Result<(), Box> { diff --git a/sdk/examples/concurrency/main.rs b/sdk/examples/concurrency/main.rs index e41e9a17..31898953 100644 --- a/sdk/examples/concurrency/main.rs +++ b/sdk/examples/concurrency/main.rs @@ -1,10 +1,10 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; use surrealdb::engine::remote::ws::Client; use surrealdb::engine::remote::ws::Ws; use surrealdb::Surreal; use tokio::sync::mpsc; -static DB: Lazy> = Lazy::new(Surreal::init); +static DB: LazyLock> = LazyLock::new(Surreal::init); const NUM: usize = 100_000; diff --git a/sdk/fuzz/Cargo.lock b/sdk/fuzz/Cargo.lock index 8457fa10..048641aa 100644 --- a/sdk/fuzz/Cargo.lock +++ b/sdk/fuzz/Cargo.lock @@ -282,17 +282,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "async-recursion" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30c5ef0ede93efbf733c1a727f3b6b5a1060bbedd5600183e66f6e4be4af0ec5" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.58", -] - [[package]] name = "async-stream" version = "0.3.5" @@ -3011,7 +3000,6 @@ dependencies = [ "futures", "geo 0.27.0", "indexmap 2.2.6", - "once_cell", "path-clean", "pharos", "reblessive", @@ -3047,7 +3035,6 @@ dependencies = [ "async-channel", "async-executor", "async-graphql", - "async-recursion", "base64 0.21.7", "bcrypt", "bincode", @@ -3079,7 +3066,6 @@ dependencies = [ "num-traits", "num_cpus", "object_store", - "once_cell", "pbkdf2", "pharos", "phf", diff --git a/sdk/src/api/engine/any/mod.rs b/sdk/src/api/engine/any/mod.rs index fc0e99ed..77d7c315 100644 --- a/sdk/src/api/engine/any/mod.rs +++ b/sdk/src/api/engine/any/mod.rs @@ -218,11 +218,11 @@ impl Surreal { /// # Examples /// /// ```no_run - /// use once_cell::sync::Lazy; + /// use std::sync::LazyLock; /// use surrealdb::Surreal; /// use surrealdb::engine::any::Any; /// - /// static DB: Lazy> = Lazy::new(Surreal::init); + /// static DB: LazyLock> = LazyLock::new(Surreal::init); /// /// # #[tokio::main] /// # async fn main() -> surrealdb::Result<()> { diff --git a/sdk/src/api/engine/local/mod.rs b/sdk/src/api/engine/local/mod.rs index 4c766a17..123edee8 100644 --- a/sdk/src/api/engine/local/mod.rs +++ b/sdk/src/api/engine/local/mod.rs @@ -92,12 +92,12 @@ const DEFAULT_TICK_INTERVAL: Duration = Duration::from_secs(10); /// Instantiating a global instance /// /// ``` -/// use once_cell::sync::Lazy; +/// use std::sync::LazyLock; /// use surrealdb::{Result, Surreal}; /// use surrealdb::engine::local::Db; /// use surrealdb::engine::local::Mem; /// -/// static DB: Lazy> = Lazy::new(Surreal::init); +/// static DB: LazyLock> = LazyLock::new(Surreal::init); /// /// #[tokio::main] /// async fn main() -> Result<()> { diff --git a/sdk/src/api/engine/remote/http/mod.rs b/sdk/src/api/engine/remote/http/mod.rs index 3ad4951d..d3ebd1f1 100644 --- a/sdk/src/api/engine/remote/http/mod.rs +++ b/sdk/src/api/engine/remote/http/mod.rs @@ -68,12 +68,12 @@ impl Surreal { /// # Examples /// /// ```no_run - /// use once_cell::sync::Lazy; + /// use std::sync::LazyLock; /// use surrealdb::Surreal; /// use surrealdb::engine::remote::http::Client; /// use surrealdb::engine::remote::http::Http; /// - /// static DB: Lazy> = Lazy::new(Surreal::init); + /// static DB: LazyLock> = LazyLock::new(Surreal::init); /// /// # #[tokio::main] /// # async fn main() -> surrealdb::Result<()> { diff --git a/sdk/src/api/engine/remote/ws/mod.rs b/sdk/src/api/engine/remote/ws/mod.rs index e9b0029c..43c83948 100644 --- a/sdk/src/api/engine/remote/ws/mod.rs +++ b/sdk/src/api/engine/remote/ws/mod.rs @@ -113,12 +113,12 @@ impl Surreal { /// # Examples /// /// ```no_run - /// use once_cell::sync::Lazy; + /// use std::sync::LazyLock; /// use surrealdb::Surreal; /// use surrealdb::engine::remote::ws::Client; /// use surrealdb::engine::remote::ws::Ws; /// - /// static DB: Lazy> = Lazy::new(Surreal::init); + /// static DB: LazyLock> = LazyLock::new(Surreal::init); /// /// # #[tokio::main] /// # async fn main() -> surrealdb::Result<()> { diff --git a/sdk/src/api/method/mod.rs b/sdk/src/api/method/mod.rs index b7646baa..b15f8e3f 100644 --- a/sdk/src/api/method/mod.rs +++ b/sdk/src/api/method/mod.rs @@ -132,7 +132,7 @@ where /// Using a static, compile-time scheme /// /// ```no_run - /// use once_cell::sync::Lazy; + /// use std::sync::LazyLock; /// use serde::{Serialize, Deserialize}; /// use surrealdb::Surreal; /// use surrealdb::opt::auth::Root; @@ -140,7 +140,7 @@ where /// use surrealdb::engine::remote::ws::Client; /// /// // Creates a new static instance of the client - /// static DB: Lazy> = Lazy::new(Surreal::init); + /// static DB: LazyLock> = LazyLock::new(Surreal::init); /// /// #[derive(Serialize, Deserialize)] /// struct Person { @@ -174,14 +174,14 @@ where /// Using a dynamic, run-time scheme /// /// ```no_run - /// use once_cell::sync::Lazy; + /// use std::sync::LazyLock; /// use serde::{Serialize, Deserialize}; /// use surrealdb::Surreal; /// use surrealdb::engine::any::Any; /// use surrealdb::opt::auth::Root; /// /// // Creates a new static instance of the client - /// static DB: Lazy> = Lazy::new(Surreal::init); + /// static DB: LazyLock> = LazyLock::new(Surreal::init); /// /// #[derive(Serialize, Deserialize)] /// struct Person { diff --git a/sdk/src/api/method/tests/mod.rs b/sdk/src/api/method/tests/mod.rs index 78556ad1..511e1989 100644 --- a/sdk/src/api/method/tests/mod.rs +++ b/sdk/src/api/method/tests/mod.rs @@ -14,16 +14,16 @@ use crate::api::opt::auth::Root; use crate::api::opt::PatchOp; use crate::api::Response as QueryResponse; use crate::api::Surreal; -use once_cell::sync::Lazy; use protocol::Client; use protocol::Test; use semver::Version; use std::ops::Bound; +use std::sync::LazyLock; use surrealdb_core::sql::statements::{BeginStatement, CommitStatement}; use types::User; use types::USER; -static DB: Lazy> = Lazy::new(Surreal::init); +static DB: LazyLock> = LazyLock::new(Surreal::init); #[tokio::test] async fn api() { diff --git a/sdk/tests/api.rs b/sdk/tests/api.rs index 13a0234f..f84fdc04 100644 --- a/sdk/tests/api.rs +++ b/sdk/tests/api.rs @@ -1,7 +1,6 @@ #[allow(unused_imports, dead_code)] mod api_integration { use chrono::DateTime; - use once_cell::sync::Lazy; use semver::Version; use serde::Deserialize; use serde::Serialize; @@ -10,6 +9,7 @@ mod api_integration { use std::borrow::Cow; use std::ops::Bound; use std::sync::Arc; + use std::sync::LazyLock; use std::sync::Mutex; use std::time::Duration; use surrealdb::error::Api as ApiError; diff --git a/src/cnf/mod.rs b/src/cnf/mod.rs index 4a5e1d3e..9cd920fc 100644 --- a/src/cnf/mod.rs +++ b/src/cnf/mod.rs @@ -1,4 +1,4 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; use std::time::Duration; use surrealdb::{lazy_env_parse, lazy_env_parse_or_else}; @@ -29,50 +29,50 @@ pub const PKG_NAME: &str = "surrealdb"; pub const APP_ENDPOINT: &str = "https://surrealdb.com/app"; /// The maximum HTTP body size of the HTTP /ml endpoints (defaults to 4 GiB) -pub static HTTP_MAX_ML_BODY_SIZE: Lazy = +pub static HTTP_MAX_ML_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_ML_BODY_SIZE", usize, 4 << 30); /// The maximum HTTP body size of the HTTP /sql endpoint (defaults to 1 MiB) -pub static HTTP_MAX_SQL_BODY_SIZE: Lazy = +pub static HTTP_MAX_SQL_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_SQL_BODY_SIZE", usize, 1 << 20); /// The maximum HTTP body size of the HTTP /rpc endpoint (defaults to 4 MiB) -pub static HTTP_MAX_RPC_BODY_SIZE: Lazy = +pub static HTTP_MAX_RPC_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_RPC_BODY_SIZE", usize, 4 << 20); /// The maximum HTTP body size of the HTTP /key endpoints (defaults to 16 KiB) -pub static HTTP_MAX_KEY_BODY_SIZE: Lazy = +pub static HTTP_MAX_KEY_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_KEY_BODY_SIZE", usize, 16 << 10); /// The maximum HTTP body size of the HTTP /signup endpoint (defaults to 1 KiB) -pub static HTTP_MAX_SIGNUP_BODY_SIZE: Lazy = +pub static HTTP_MAX_SIGNUP_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_SIGNUP_BODY_SIZE", usize, 1 << 10); /// The maximum HTTP body size of the HTTP /signin endpoint (defaults to 1 KiB) -pub static HTTP_MAX_SIGNIN_BODY_SIZE: Lazy = +pub static HTTP_MAX_SIGNIN_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_SIGNIN_BODY_SIZE", usize, 1 << 10); /// The maximum HTTP body size of the HTTP /import endpoint (defaults to 4 GiB) -pub static HTTP_MAX_IMPORT_BODY_SIZE: Lazy = +pub static HTTP_MAX_IMPORT_BODY_SIZE: LazyLock = lazy_env_parse!("SURREAL_HTTP_MAX_IMPORT_BODY_SIZE", usize, 4 << 30); /// Specifies the frequency with which ping messages should be sent to the client pub const WEBSOCKET_PING_FREQUENCY: Duration = Duration::from_secs(5); /// What is the maximum WebSocket frame size (defaults to 16 MiB) -pub static WEBSOCKET_MAX_FRAME_SIZE: Lazy = +pub static WEBSOCKET_MAX_FRAME_SIZE: LazyLock = lazy_env_parse!("SURREAL_WEBSOCKET_MAX_FRAME_SIZE", usize, 16 << 20); /// What is the maximum WebSocket message size (defaults to 128 MiB) -pub static WEBSOCKET_MAX_MESSAGE_SIZE: Lazy = +pub static WEBSOCKET_MAX_MESSAGE_SIZE: LazyLock = lazy_env_parse!("SURREAL_WEBSOCKET_MAX_MESSAGE_SIZE", usize, 128 << 20); /// How many concurrent tasks can be handled on each WebSocket (defaults to 24) -pub static WEBSOCKET_MAX_CONCURRENT_REQUESTS: Lazy = +pub static WEBSOCKET_MAX_CONCURRENT_REQUESTS: LazyLock = lazy_env_parse!("SURREAL_WEBSOCKET_MAX_CONCURRENT_REQUESTS", usize, 24); /// What is the runtime thread memory stack size (defaults to 10MiB) -pub static RUNTIME_STACK_SIZE: Lazy = +pub static RUNTIME_STACK_SIZE: LazyLock = lazy_env_parse_or_else!("SURREAL_RUNTIME_STACK_SIZE", usize, |_| { // Stack frames are generally larger in debug mode. if cfg!(debug_assertions) { @@ -83,17 +83,18 @@ pub static RUNTIME_STACK_SIZE: Lazy = }); /// How many threads which can be started for blocking operations (defaults to 512) -pub static RUNTIME_MAX_BLOCKING_THREADS: Lazy = +pub static RUNTIME_MAX_BLOCKING_THREADS: LazyLock = lazy_env_parse!("SURREAL_RUNTIME_MAX_BLOCKING_THREADS", usize, 512); /// The version identifier of this build -pub static PKG_VERSION: Lazy = Lazy::new(|| match option_env!("SURREAL_BUILD_METADATA") { - Some(metadata) if !metadata.trim().is_empty() => { - let version = env!("CARGO_PKG_VERSION"); - format!("{version}+{metadata}") - } - _ => env!("CARGO_PKG_VERSION").to_owned(), -}); +pub static PKG_VERSION: LazyLock = + LazyLock::new(|| match option_env!("SURREAL_BUILD_METADATA") { + Some(metadata) if !metadata.trim().is_empty() => { + let version = env!("CARGO_PKG_VERSION"); + format!("{version}+{metadata}") + } + _ => env!("CARGO_PKG_VERSION").to_owned(), + }); -pub static GRAPHQL_ENABLE: Lazy = +pub static GRAPHQL_ENABLE: LazyLock = lazy_env_parse!("SURREAL_EXPERIMENTAL_GRAPHQL", bool, false); diff --git a/src/env/mod.rs b/src/env/mod.rs index 148486d6..cfb97d18 100644 --- a/src/env/mod.rs +++ b/src/env/mod.rs @@ -1,11 +1,11 @@ use crate::cnf::PKG_VERSION; use crate::err::Error; -use once_cell::sync::Lazy; +use std::sync::LazyLock; use surrealdb::env::{arch, os}; /// Stores the current release identifier -pub static RELEASE: Lazy = - Lazy::new(|| format!("{} for {} on {}", *PKG_VERSION, os(), arch())); +pub static RELEASE: LazyLock = + LazyLock::new(|| format!("{} for {} on {}", *PKG_VERSION, os(), arch())); pub async fn init() -> Result<(), Error> { // Log version diff --git a/src/telemetry/metrics/http/mod.rs b/src/telemetry/metrics/http/mod.rs index 214bfe4a..e8234556 100644 --- a/src/telemetry/metrics/http/mod.rs +++ b/src/telemetry/metrics/http/mod.rs @@ -1,14 +1,14 @@ pub(super) mod tower_layer; -use once_cell::sync::Lazy; use opentelemetry::global; use opentelemetry::metrics::{Histogram, Meter, MetricsError, UpDownCounter}; +use std::sync::LazyLock; use self::tower_layer::HttpCallMetricTracker; -static METER: Lazy = Lazy::new(|| global::meter("surrealdb.http")); +static METER: LazyLock = LazyLock::new(|| global::meter("surrealdb.http")); -pub static HTTP_SERVER_DURATION: Lazy> = Lazy::new(|| { +pub static HTTP_SERVER_DURATION: LazyLock> = LazyLock::new(|| { METER .u64_histogram("http.server.duration") .with_description("The HTTP server duration in milliseconds.") @@ -16,14 +16,14 @@ pub static HTTP_SERVER_DURATION: Lazy> = Lazy::new(|| { .init() }); -pub static HTTP_SERVER_ACTIVE_REQUESTS: Lazy> = Lazy::new(|| { +pub static HTTP_SERVER_ACTIVE_REQUESTS: LazyLock> = LazyLock::new(|| { METER .i64_up_down_counter("http.server.active_requests") .with_description("The number of active HTTP requests.") .init() }); -pub static HTTP_SERVER_REQUEST_SIZE: Lazy> = Lazy::new(|| { +pub static HTTP_SERVER_REQUEST_SIZE: LazyLock> = LazyLock::new(|| { METER .u64_histogram("http.server.request.size") .with_description("Measures the size of HTTP request messages.") @@ -31,7 +31,7 @@ pub static HTTP_SERVER_REQUEST_SIZE: Lazy> = Lazy::new(|| { .init() }); -pub static HTTP_SERVER_RESPONSE_SIZE: Lazy> = Lazy::new(|| { +pub static HTTP_SERVER_RESPONSE_SIZE: LazyLock> = LazyLock::new(|| { METER .u64_histogram("http.server.response.size") .with_description("Measures the size of HTTP response messages.") diff --git a/src/telemetry/metrics/ws/mod.rs b/src/telemetry/metrics/ws/mod.rs index fa4200a3..d0e121f6 100644 --- a/src/telemetry/metrics/ws/mod.rs +++ b/src/telemetry/metrics/ws/mod.rs @@ -1,16 +1,16 @@ use std::time::Instant; -use once_cell::sync::Lazy; use opentelemetry::metrics::Meter; use opentelemetry::{global, KeyValue}; use opentelemetry::{ metrics::{Histogram, MetricsError, UpDownCounter}, Context as TelemetryContext, }; +use std::sync::LazyLock; -static METER: Lazy = Lazy::new(|| global::meter("surrealdb.rpc")); +static METER: LazyLock = LazyLock::new(|| global::meter("surrealdb.rpc")); -pub static RPC_SERVER_DURATION: Lazy> = Lazy::new(|| { +pub static RPC_SERVER_DURATION: LazyLock> = LazyLock::new(|| { METER .u64_histogram("rpc.server.duration") .with_description("Measures duration of inbound RPC requests in milliseconds.") @@ -18,14 +18,14 @@ pub static RPC_SERVER_DURATION: Lazy> = Lazy::new(|| { .init() }); -pub static RPC_SERVER_ACTIVE_CONNECTIONS: Lazy> = Lazy::new(|| { +pub static RPC_SERVER_ACTIVE_CONNECTIONS: LazyLock> = LazyLock::new(|| { METER .i64_up_down_counter("rpc.server.active_connections") .with_description("The number of active WebSocket connections.") .init() }); -pub static RPC_SERVER_REQUEST_SIZE: Lazy> = Lazy::new(|| { +pub static RPC_SERVER_REQUEST_SIZE: LazyLock> = LazyLock::new(|| { METER .u64_histogram("rpc.server.request.size") .with_description("Measures the size of HTTP request messages.") @@ -33,7 +33,7 @@ pub static RPC_SERVER_REQUEST_SIZE: Lazy> = Lazy::new(|| { .init() }); -pub static RPC_SERVER_RESPONSE_SIZE: Lazy> = Lazy::new(|| { +pub static RPC_SERVER_RESPONSE_SIZE: LazyLock> = LazyLock::new(|| { METER .u64_histogram("rpc.server.response.size") .with_description("Measures the size of HTTP response messages.") diff --git a/src/telemetry/mod.rs b/src/telemetry/mod.rs index f1e59086..d839f04b 100644 --- a/src/telemetry/mod.rs +++ b/src/telemetry/mod.rs @@ -3,7 +3,6 @@ pub mod metrics; pub mod traces; use crate::cli::validator::parser::env_filter::CustomEnvFilter; -use once_cell::sync::Lazy; use opentelemetry::metrics::MetricsError; use opentelemetry::Context; use opentelemetry::KeyValue; @@ -11,6 +10,7 @@ use opentelemetry_sdk::resource::{ EnvResourceDetector, SdkProvidedResourceDetector, TelemetryResourceDetector, }; use opentelemetry_sdk::Resource; +use std::sync::LazyLock; use std::time::Duration; use tracing::{Level, Subscriber}; use tracing_subscriber::filter::ParseError; @@ -18,7 +18,7 @@ use tracing_subscriber::prelude::*; use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; -pub static OTEL_DEFAULT_RESOURCE: Lazy = Lazy::new(|| { +pub static OTEL_DEFAULT_RESOURCE: LazyLock = LazyLock::new(|| { let res = Resource::from_detectors( Duration::from_secs(5), vec![