Skip to content

Commit

Permalink
Server: Replace lazy static with once cell everywhere (#1024)
Browse files Browse the repository at this point in the history
The `lazy_static` crate has not received maintenance in about four
years, and has generally been deprecated in favor of `once_cell`. The
latter's interface is being incorporated into the standard library.

This PR simply swaps out `lazy_static` macro invocations for
`once_cell`'s `Lazy` interface.
  • Loading branch information
svix-nick authored Jul 26, 2023
1 parent feb72f2 commit 827c944
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/Cargo.lock

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

2 changes: 1 addition & 1 deletion server/svix-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ serde_path_to_error = "0.1.7"
num_enum = "0.5.6"
enum_dispatch = "0.3.8"
regex = "1.5.5"
lazy_static = "1.4.0"
once_cell = "1.18.0"
figment = { version = "0.10", features = ["toml", "env", "test"] }
tracing = "0.1.35"
tracing-subscriber = { version="0.3", features = ["env-filter", "json"] }
Expand Down
6 changes: 2 additions & 4 deletions server/svix-server/src/core/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use std::collections::{HashMap, HashSet};

use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use rand::Rng;

use regex::Regex;

use once_cell::sync::Lazy;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
Expand Down Expand Up @@ -217,9 +217,7 @@ pub trait BaseId: Deref<Target = String> {

fn validate_limited_str(s: &str) -> std::result::Result<(), ValidationErrors> {
const MAX_LENGTH: usize = 256;
lazy_static! {
static ref RE: Regex = Regex::new(r"^[a-zA-Z0-9\-_.]+$").unwrap();
}
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9\-_.]+$").unwrap());
let mut errors = ValidationErrors::new();
if s.is_empty() {
errors.add(
Expand Down
5 changes: 1 addition & 4 deletions server/svix-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use aide::axum::ApiRouter;

use crate::core::cache::Cache;
use cfg::ConfigurationInner;
use lazy_static::lazy_static;
use opentelemetry::runtime::Tokio;
use opentelemetry_otlp::WithExportConfig;
use queue::TaskQueueProducer;
Expand Down Expand Up @@ -47,9 +46,7 @@ pub mod worker;

const CRATE_NAME: &str = env!("CARGO_CRATE_NAME");

lazy_static! {
pub static ref SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);
}
pub static SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);

async fn graceful_shutdown_handler() {
let ctrl_c = async {
Expand Down
8 changes: 4 additions & 4 deletions server/svix-server/src/v1/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use axum::{
};
use chrono::{DateTime, Utc};
use http::{request::Parts, Request, StatusCode};
use once_cell::sync::Lazy;
use regex::Regex;
use schemars::JsonSchema;
use sea_orm::{ColumnTrait, QueryFilter, QueryOrder, QuerySelect};
Expand All @@ -46,9 +47,8 @@ const fn default_limit() -> PaginationLimit {

const PAGINATION_LIMIT_CAP_HARD: bool = true;
const PAGINATION_LIMIT_CAP_LIMIT: u64 = 250;
// TODO: Should probably use lazy_static and format! to make this instead of repeating the 250
// figure at some point
const PAGINATION_LIMIT_ERROR: &str = "Given limit must not exceed 250";
static PAGINATION_LIMIT_ERROR: Lazy<String> =
Lazy::new(|| format!("Given limit must not exceed {PAGINATION_LIMIT_CAP_LIMIT}"));

#[derive(Debug, Deserialize, Validate, JsonSchema)]
pub struct PaginationDescending<T: Validate + JsonSchema> {
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Validate for PaginationLimit {
if self.0 > PAGINATION_LIMIT_CAP_LIMIT {
errs.add(
"limit",
validation_error(Some("pagination"), Some(PAGINATION_LIMIT_ERROR)),
validation_error(Some("pagination"), Some(&PAGINATION_LIMIT_ERROR)),
);
}

Expand Down
6 changes: 2 additions & 4 deletions server/svix-server/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use chrono::Utc;

use futures::future;
use http::{HeaderValue, StatusCode, Version};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use rand::Rng;

use sea_orm::prelude::DateTimeUtc;
Expand Down Expand Up @@ -855,9 +855,7 @@ async fn process_queue_task_inner(
Ok(())
}

lazy_static! {
pub static ref LAST_QUEUE_POLL: AtomicU64 = get_unix_timestamp().into();
}
pub static LAST_QUEUE_POLL: Lazy<AtomicU64> = Lazy::new(|| get_unix_timestamp().into());

async fn update_last_poll_time() {
LAST_QUEUE_POLL.swap(get_unix_timestamp(), Ordering::Relaxed);
Expand Down

0 comments on commit 827c944

Please sign in to comment.