Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static and once_cell with std library equivalents #1212

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions mountpoint-s3-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ auto_impl = "1.2.0"
base64ct = { version = "1.6.0", features = ["std"] }
const_format = "0.2.34"
futures = "0.3.31"
lazy_static = "1.5.0"
metrics = "0.24.1"
once_cell = "1.20.2"
percent-encoding = "2.3.1"
pin-project = "1.1.7"
platform-info = "2.0.4"
Expand Down
8 changes: 3 additions & 5 deletions mountpoint-s3-client/src/endpoint_config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::os::unix::prelude::OsStrExt;
use std::sync::LazyLock;

use lazy_static::lazy_static;
use mountpoint_s3_crt::{
auth::signing_config::SigningAlgorithm,
common::{allocator::Allocator, uri::Uri},
s3::endpoint_resolver::{RequestContext, ResolvedEndpoint, ResolverError, RuleEngine},
};
use thiserror::Error;

lazy_static! {
// A static s3 endpoint rule engine that can be shared across s3 client
static ref S3_ENDPOINT_RULE_ENGINE: RuleEngine = RuleEngine::new(&Default::default()).unwrap();
}
/// A static s3 endpoint rule engine that can be shared across s3 client
static S3_ENDPOINT_RULE_ENGINE: LazyLock<RuleEngine> = LazyLock::new(|| RuleEngine::new(&Default::default()).unwrap());

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AddressingStyle {
Expand Down
6 changes: 3 additions & 3 deletions mountpoint-s3-client/src/instance_info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A simple interface to retrieve information about the EC2 instance this client is running on by
//! querying the Instance Metadata Service (IMDS).

use std::cell::LazyCell;
use std::env;

use once_cell::unsync::Lazy;
use thiserror::Error;

use crate::imds_crt_client::{IdentityDocument, ImdsCrtClient, ImdsQueryRequestError};
Expand All @@ -12,15 +12,15 @@ use crate::imds_crt_client::{IdentityDocument, ImdsCrtClient, ImdsQueryRequestEr
/// the `AWS_EC2_METADATA_DISABLED` environment variable is not set.
#[derive(Debug)]
pub struct InstanceInfo {
document: Lazy<Result<IdentityDocument, InstanceInfoError>>,
document: LazyCell<Result<IdentityDocument, InstanceInfoError>>,
}

impl InstanceInfo {
/// Create a new instance. The IMDS client will only be queried when a method on the instance is
/// called, and only if the `AWS_EC2_METADATA_DISABLED` environment variable is not set.
pub fn new() -> Self {
Self {
document: Lazy::new(|| {
document: LazyCell::new(|| {
if !imds_disabled() {
match retrieve_instance_identity_document() {
Ok(identity_document) => {
Expand Down
11 changes: 4 additions & 7 deletions mountpoint-s3-client/src/mock_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Write;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use std::sync::{Arc, LazyLock, RwLock};
use std::task::{Context, Poll};
use std::time::{Duration, SystemTime};

use async_trait::async_trait;
use futures::{Stream, StreamExt};
use lazy_static::lazy_static;
use mountpoint_s3_crt::s3::client::BufferPoolUsageStats;
use rand::seq::SliceRandom;
use rand::SeedableRng;
Expand Down Expand Up @@ -54,11 +53,9 @@ pub fn ramp_bytes(seed: usize, size: usize) -> Vec<u8> {
bytes
}

lazy_static! {
// Generate bytes for ramping pattern (currently, just a simple linear ramp).
// Request RAMP_MODULUS extra bytes so we can read from any offset (modulo RAMP_MODULUS)
static ref RAMP_BYTES: Vec<u8> = ramp_bytes(0, RAMP_BUFFER_SIZE + RAMP_MODULUS);
}
/// Generate bytes for ramping pattern (currently, just a simple linear ramp).
/// Request RAMP_MODULUS extra bytes so we can read from any offset (modulo RAMP_MODULUS)
static RAMP_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| ramp_bytes(0, RAMP_BUFFER_SIZE + RAMP_MODULUS));

#[derive(Debug, Default, Clone)]
pub struct MockClientConfig {
Expand Down
22 changes: 12 additions & 10 deletions mountpoint-s3-client/src/s3_crt_client/head_object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, LazyLock, Mutex};

use lazy_static::lazy_static;
use mountpoint_s3_crt::http::request_response::{Header, Headers, HeadersError};
use mountpoint_s3_crt::s3::client::MetaRequestResult;
use regex::Regex;
Expand Down Expand Up @@ -33,14 +32,17 @@ pub enum ParseError {
InvalidRestore(String),
}

lazy_static! {
// Example: ongoing-request="true"
static ref RESTORE_IN_PROGRESS_RE: Regex = Regex::new(r#"^ongoing-request="(?<ongoing>[^"]*)"$"#).unwrap();

// Example: ongoing-request="false", expiry-date="Fri, 21 Dec 2012 00:00:00 GMT"
static ref RESTORE_DONE_RE: Regex =
Regex::new(r#"^ongoing-request="[^"]*",\s*expiry-date="(?<expiry>[^"]*)"$"#).unwrap();
}
/// Regex for determining if a restore is ongoing.
///
/// Example: `ongoing-request="true"`
static RESTORE_IN_PROGRESS_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"^ongoing-request="(?<ongoing>[^"]*)"$"#).unwrap());

/// Regex for determining a restore is complete.
///
/// Example: `ongoing-request="false", expiry-date="Fri, 21 Dec 2012 00:00:00 GMT"`
static RESTORE_DONE_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"^ongoing-request="[^"]*",\s*expiry-date="(?<expiry>[^"]*)"$"#).unwrap());

impl HeadObjectResult {
fn parse_restore_status(headers: &Headers) -> Result<Option<RestoreStatus>, ParseError> {
Expand Down
5 changes: 2 additions & 3 deletions mountpoint-s3-client/tests/common/tracing_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, LazyLock, Mutex};

use once_cell::sync::Lazy;
use tracing::{Event, Level, Subscriber};
use tracing_subscriber::field::VisitOutput;
use tracing_subscriber::fmt::format::{DefaultVisitor, Writer};
use tracing_subscriber::layer::Context;
use tracing_subscriber::Layer;

static TRACING_TEST_LAYER: Lazy<TracingTestLayer> = Lazy::new(TracingTestLayer::new);
static TRACING_TEST_LAYER: LazyLock<TracingTestLayer> = LazyLock::new(TracingTestLayer::new);

/// This is a singleton [tracing::Layer] that can be used to write tests for log events.
///
Expand Down
Loading