Skip to content

Commit

Permalink
wip: remove once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Jan 5, 2025
1 parent 3a38440 commit 38b08ee
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 70 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ moka = { version = "0.12", features = ["future", "sync"] }
nano-id = { version = "0.4", features = ["base62"] }
nix = { version = "0.29", features = ["mount", "hostname", "signal"] }
num_cpus = "1"
once_cell = "1"
regex = "1"
quick-js = { version = "0.4", features = ["patched"] }
reqwest = { version = "0.12", features = ["stream", "multipart"] }
Expand Down
11 changes: 6 additions & 5 deletions src/cgroup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::{
io::{BufRead, BufReader},
path::PathBuf,
process,
sync::LazyLock,
};

use anyhow::{Context, Result, bail};
use libcgroups::common::{CgroupSetup, get_cgroup_setup, read_cgroup_file, write_cgroup_file_str};
use once_cell::sync::Lazy;
use tracing::debug;

pub use self::utils::*;
Expand All @@ -26,17 +26,18 @@ mod utils;

const MANDATORY_CONTROLLERS: &str = "+cpu +cpuset +memory +io +pids";

pub static CGROUP_PATH: Lazy<PathBuf> = Lazy::new(|| match &conf::CONFIG.work_mode {
pub static CGROUP_PATH: LazyLock<PathBuf> = LazyLock::new(|| match &conf::CONFIG.work_mode {
SeeleWorkMode::Bare => {
systemd::create_and_enter_cgroup().expect("Error entering cgroup scope cgroup")
}
_ => utils::check_and_get_self_cgroup().expect("Error getting process' cgroup path"),
});

pub static CGROUP_MAIN_SCOPE_PATH: Lazy<PathBuf> = Lazy::new(|| CGROUP_PATH.join("main.scope"));
pub static CGROUP_MAIN_SCOPE_PATH: LazyLock<PathBuf> =
LazyLock::new(|| CGROUP_PATH.join("main.scope"));

pub static CGROUP_CONTAINER_SLICE_PATH: Lazy<PathBuf> =
Lazy::new(|| CGROUP_PATH.join("container.slice"));
pub static CGROUP_CONTAINER_SLICE_PATH: LazyLock<PathBuf> =
LazyLock::new(|| CGROUP_PATH.join("container.slice"));

#[inline]
pub fn check_cgroup_setup() -> Result<()> {
Expand Down
5 changes: 2 additions & 3 deletions src/composer/report.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{collections::HashMap, path::Path};
use std::{collections::HashMap, path::Path, sync::LazyLock};

use anyhow::{Context, Result, bail};
use futures_util::future;
use once_cell::sync::Lazy;
use reqwest::{
Client,
multipart::{Form, Part},
Expand Down Expand Up @@ -64,7 +63,7 @@ pub async fn apply_embeds_config(
}))
}

static HTTP_CLIENT: Lazy<Client> = Lazy::new(shared::http::build_http_client);
static HTTP_CLIENT: LazyLock<Client> = LazyLock::new(shared::http::build_http_client);

#[instrument(skip_all)]
pub async fn apply_uploads_config(
Expand Down
18 changes: 9 additions & 9 deletions src/conf/env.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::env;
use std::{env, sync::LazyLock};

use once_cell::sync::Lazy;
pub static HOSTNAME: LazyLock<String> =
LazyLock::new(|| env::var("HOSTNAME").unwrap_or(get_hostname()));
pub static CONTAINER_NAME: LazyLock<Option<String>> =
LazyLock::new(|| env::var("CONTAINER_NAME").ok());
pub static CONTAINER_IMAGE_NAME: LazyLock<Option<String>> =
LazyLock::new(|| env::var("CONTAINER_IMAGE_NAME").ok());

pub static HOSTNAME: Lazy<String> = Lazy::new(|| env::var("HOSTNAME").unwrap_or(get_hostname()));
pub static CONTAINER_NAME: Lazy<Option<String>> = Lazy::new(|| env::var("CONTAINER_NAME").ok());
pub static CONTAINER_IMAGE_NAME: Lazy<Option<String>> =
Lazy::new(|| env::var("CONTAINER_IMAGE_NAME").ok());

pub static COMMIT_TAG: Lazy<Option<&'static str>> = Lazy::new(|| option_env!("COMMIT_TAG"));
pub static COMMIT_SHA: Lazy<Option<&'static str>> = Lazy::new(|| option_env!("COMMIT_SHA"));
pub static COMMIT_TAG: LazyLock<Option<&'static str>> = LazyLock::new(|| option_env!("COMMIT_TAG"));
pub static COMMIT_SHA: LazyLock<Option<&'static str>> = LazyLock::new(|| option_env!("COMMIT_SHA"));

#[inline]
fn get_hostname() -> String {
Expand Down
5 changes: 2 additions & 3 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::PathBuf;
use std::{path::PathBuf, sync::LazyLock};

use indexmap::IndexMap;
use once_cell::sync::Lazy;
use serde::Deserialize;
use tracing_subscriber::filter::LevelFilter;

Expand All @@ -21,7 +20,7 @@ mod path;
mod telemetry;
mod worker;

pub static CONFIG: Lazy<SeeleConfig> = Lazy::new(|| {
pub static CONFIG: LazyLock<SeeleConfig> = LazyLock::new(|| {
match config::Config::builder()
.add_source(config::File::with_name("config"))
.add_source(config::Environment::with_prefix("SEELE"))
Expand Down
5 changes: 2 additions & 3 deletions src/conf/path.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::PathBuf;
use std::{path::PathBuf, sync::LazyLock};

use anyhow::{Context, Result};
use once_cell::sync::Lazy;
use tokio::fs;

use super::CONFIG;
Expand All @@ -24,7 +23,7 @@ impl SeelePaths {
}
}

pub static PATHS: Lazy<SeelePaths> = Lazy::new(|| SeelePaths {
pub static PATHS: LazyLock<SeelePaths> = LazyLock::new(|| SeelePaths {
root: CONFIG.paths.root.clone(),
images: CONFIG.paths.root.join("images"),
temp: CONFIG.paths.root.join("temp"),
Expand Down
10 changes: 7 additions & 3 deletions src/exchange/amqp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::{collections::HashMap, num::NonZeroUsize, sync::Arc, time::Duration};
use std::{
collections::HashMap,
num::NonZeroUsize,
sync::{Arc, LazyLock},
time::Duration,
};

use anyhow::{Context, Result, bail};
use futures_util::StreamExt;
use lapin::{Channel, ChannelState, Connection, message::Delivery};
use once_cell::sync::Lazy;
use ring_channel::ring_channel;
use tokio::{
sync::{Mutex, mpsc},
Expand All @@ -18,7 +22,7 @@ use crate::{
conf::{self, AmqpExchangeConfig, AmqpExchangeReportConfig},
};

static STATUS_MAP: Lazy<Mutex<HashMap<String, bool>>> = Lazy::new(Default::default);
static STATUS_MAP: LazyLock<Mutex<HashMap<String, bool>>> = LazyLock::new(Default::default);

pub async fn is_amqp_healthy() -> bool {
let map = STATUS_MAP.lock().await;
Expand Down
28 changes: 14 additions & 14 deletions src/shared/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::atomic::Ordering;
use std::sync::{LazyLock, OnceLock, atomic::Ordering};

use anyhow::Result;
use once_cell::sync::{Lazy, OnceCell};
use opentelemetry::{
KeyValue, global,
metrics::{Histogram, Meter, ObservableGauge, Unit},
Expand All @@ -11,7 +10,7 @@ use opentelemetry::{
use super::runner;
use crate::conf;

pub static METRICS_RESOURCE: Lazy<Resource> = Lazy::new(|| {
pub static METRICS_RESOURCE: LazyLock<Resource> = LazyLock::new(|| {
let mut pairs = vec![
KeyValue::new("service.name", "seele"),
KeyValue::new(
Expand Down Expand Up @@ -44,32 +43,33 @@ pub static METRICS_RESOURCE: Lazy<Resource> = Lazy::new(|| {
Resource::new(pairs)
});

pub static METRICS_CONTROLLER: OnceCell<BasicController> = OnceCell::new();
pub static METRICS_CONTROLLER: OnceLock<BasicController> = OnceLock::new();

pub static METER: Lazy<Meter> =
Lazy::new(|| global::meter_with_version("seele", Some("0.1"), None));
pub static METER: LazyLock<Meter> =
LazyLock::new(|| global::meter_with_version("seele", Some("0.1"), None));

pub static SUBMISSION_HANDLING_HISTOGRAM: Lazy<Histogram<f64>> = Lazy::new(|| {
pub static SUBMISSION_HANDLING_HISTOGRAM: LazyLock<Histogram<f64>> = LazyLock::new(|| {
METER
.f64_histogram("seele.submission.duration")
.with_description("Duration of submissions handling")
.with_unit(Unit::new("s"))
.init()
});

pub static RUNNER_COUNT_GAUGE: Lazy<ObservableGauge<u64>> = Lazy::new(|| {
pub static RUNNER_COUNT_GAUGE: LazyLock<ObservableGauge<u64>> = LazyLock::new(|| {
METER
.u64_observable_gauge("seele.runner.count")
.with_description("Count of available runner threads")
.init()
});

pub static PENDING_CONTAINER_ACTION_COUNT_GAUGE: Lazy<ObservableGauge<u64>> = Lazy::new(|| {
METER
.u64_observable_gauge("seele.action.container.pending.count")
.with_description("Count of pending container actions in the worker queue")
.init()
});
pub static PENDING_CONTAINER_ACTION_COUNT_GAUGE: LazyLock<ObservableGauge<u64>> =
LazyLock::new(|| {
METER
.u64_observable_gauge("seele.action.container.pending.count")
.with_description("Count of pending container actions in the worker queue")
.init()
});

pub fn register_gauge_metrics() -> Result<()> {
METER.register_callback(|ctx| {
Expand Down
5 changes: 2 additions & 3 deletions src/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{env, io::SeekFrom};
use std::{env, io::SeekFrom, sync::LazyLock};

use anyhow::Result;
use once_cell::sync::Lazy;
use tokio::{
fs::File,
io::{AsyncReadExt, AsyncSeekExt, BufReader},
Expand All @@ -14,7 +13,7 @@ pub mod image;
pub mod metrics;
pub mod runner;

pub static TINI_PRESENTS: Lazy<bool> = Lazy::new(|| env::var_os("TINI_VERSION").is_some());
pub static TINI_PRESENTS: LazyLock<bool> = LazyLock::new(|| env::var_os("TINI_VERSION").is_some());

pub static ABORTED_MESSAGE: &str = "Aborted due to shutting down";

Expand Down
11 changes: 7 additions & 4 deletions src/shared/runner.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{
LazyLock,
atomic::{AtomicU64, Ordering},
};

use once_cell::sync::Lazy;
use tokio::{
sync::Semaphore,
task::{self, JoinError},
};

use crate::conf;

pub static PENDING_TASKS: Lazy<AtomicU64> = Lazy::new(|| AtomicU64::new(0));
pub static PENDING_TASKS: LazyLock<AtomicU64> = LazyLock::new(|| AtomicU64::new(0));

static RUNNERS: Lazy<Semaphore> = Lazy::new(|| Semaphore::new(conf::CONFIG.thread_counts.runner));
static RUNNERS: LazyLock<Semaphore> =
LazyLock::new(|| Semaphore::new(conf::CONFIG.thread_counts.runner));

pub async fn spawn_blocking<F, R>(f: F) -> Result<R, JoinError>
where
Expand Down
5 changes: 2 additions & 3 deletions src/worker/action/add_file/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{
path::{Path, PathBuf},
sync::Arc,
sync::{Arc, LazyLock},
};

use anyhow::{Context, Result, bail};
use bytes::Bytes;
use futures_util::{Stream, StreamExt, future};
use http_cache::HttpCacheOptions;
use once_cell::sync::Lazy;
use tokio::{
fs::File,
io::{self, AsyncWriteExt},
Expand Down Expand Up @@ -92,7 +91,7 @@ async fn handle_local_path(mut file: File, path: &Path) -> Result<()> {
Ok(())
}

static HTTP_CLIENT: Lazy<reqwest_middleware::ClientWithMiddleware> = Lazy::new(|| {
static HTTP_CLIENT: LazyLock<reqwest_middleware::ClientWithMiddleware> = LazyLock::new(|| {
use std::time::Duration;

use http_cache::MokaManager;
Expand Down
8 changes: 5 additions & 3 deletions src/worker/action/run_container/cache.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{sync::Arc, time::Duration};
use std::{
sync::{Arc, LazyLock},
time::Duration,
};

use moka::sync::Cache;
use once_cell::sync::Lazy;

use crate::conf;

#[allow(clippy::type_complexity)]
static CACHE: Lazy<Cache<Box<[u8]>, Arc<[u8]>>> = Lazy::new(|| {
static CACHE: LazyLock<Cache<Box<[u8]>, Arc<[u8]>>> = LazyLock::new(|| {
let config = &conf::CONFIG.worker.action.run_container;
Cache::builder()
.name("seele-run-container")
Expand Down
10 changes: 5 additions & 5 deletions src/worker/action/run_container/idmap.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
fs::File,
io::{BufRead, BufReader},
sync::LazyLock,
};

use anyhow::{Context, Result, bail};
use once_cell::sync::Lazy;

use crate::conf;

Expand All @@ -16,11 +16,11 @@ pub struct SubIds {
pub count: u32,
}

pub static SUBUIDS: Lazy<SubIds> =
Lazy::new(|| get_subids(SUBUID_PATH).expect("Error getting subuids"));
pub static SUBUIDS: LazyLock<SubIds> =
LazyLock::new(|| get_subids(SUBUID_PATH).expect("Error getting subuids"));

pub static SUBGIDS: Lazy<SubIds> =
Lazy::new(|| get_subids(SUBGID_PATH).expect("Error getting subgids"));
pub static SUBGIDS: LazyLock<SubIds> =
LazyLock::new(|| get_subids(SUBGID_PATH).expect("Error getting subgids"));

fn get_subids(path: &str) -> Result<SubIds> {
let username = &conf::CONFIG.worker.action.run_container.userns_user;
Expand Down
12 changes: 8 additions & 4 deletions src/worker/action/run_container/image.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::{
fs::Permissions, os::unix::prelude::PermissionsExt, path::PathBuf, sync::Arc, time::Duration,
fs::Permissions,
os::unix::prelude::PermissionsExt,
path::PathBuf,
sync::{Arc, LazyLock},
time::Duration,
};

use anyhow::{Context, Result, bail};
Expand All @@ -9,7 +13,6 @@ use nix::{
sys::signal::{self, Signal},
unistd::Pid,
};
use once_cell::sync::Lazy;
use tokio::{
fs::{self, create_dir_all, metadata, remove_dir_all},
sync::oneshot,
Expand All @@ -23,8 +26,9 @@ use crate::{
shared::{self, cond::CondGroup, image::OciImage, runner},
};

static PREPARATION_TASKS: Lazy<CondGroup<OciImage, Result<(), String>>> =
Lazy::new(|| CondGroup::new(|payload: &OciImage| prepare_image_impl(payload.clone()).boxed()));
static PREPARATION_TASKS: LazyLock<CondGroup<OciImage, Result<(), String>>> = LazyLock::new(|| {
CondGroup::new(|payload: &OciImage| prepare_image_impl(payload.clone()).boxed())
});

pub async fn prepare_image(abort: Listener, image: OciImage) -> Result<()> {
match PREPARATION_TASKS.run(image, abort).await {
Expand Down
8 changes: 5 additions & 3 deletions src/worker/action/run_container/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{io::Read, sync::Arc};
use std::{
io::Read,
sync::{Arc, LazyLock},
};

use anyhow::{Context, Result, bail};
use duct::cmd;
use nix::{
sys::signal::{self, Signal},
unistd::Pid,
};
use once_cell::sync::Lazy;
use thread_local::ThreadLocal;
use tokio::sync::oneshot;
use tracing::{Span, info, info_span, warn};
Expand All @@ -33,7 +35,7 @@ pub mod run_judge;
mod runj;
mod utils;

static RUNNER_THREAD_LOCAL: Lazy<Arc<ThreadLocal<i64>>> = Lazy::new(Arc::default);
static RUNNER_THREAD_LOCAL: LazyLock<Arc<ThreadLocal<i64>>> = LazyLock::new(Arc::default);

pub async fn execute(
abort: Listener,
Expand Down
Loading

0 comments on commit 38b08ee

Please sign in to comment.