Skip to content

Commit c239bcf

Browse files
KixironJoshua Nelson
authored andcommitted
Removed all usages of lazy_static and replaced them with once_cell
1 parent 2a297ce commit c239bcf

File tree

6 files changed

+103
-49
lines changed

6 files changed

+103
-49
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ futures = "0.1"
3737
tokio = "0.1"
3838
systemstat = "0.1.4"
3939
prometheus = { version = "0.7.0", default-features = false }
40-
lazy_static = "1.0.0"
4140
rustwide = "0.7.1"
4241
mime_guess = "2"
4342
dotenv = "0.15"
4443
zstd = "0.5"
4544
git2 = { version = "0.13.6", default-features = false }
46-
once_cell = "1.4.0"
4745
path-slash = "0.1.3"
46+
once_cell = { version = "1.4.0", features = ["parking_lot"] }
4847

4948
# Data serialization and deserialization
5049
serde = { version = "1.0", features = ["derive"] }

src/web/badge/test.svg

Lines changed: 23 additions & 0 deletions
Loading

src/web/metrics.rs

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,119 +3,149 @@ use crate::BuildQueue;
33
use iron::headers::ContentType;
44
use iron::prelude::*;
55
use iron::status::Status;
6+
use once_cell::sync::Lazy;
67
use prometheus::{
78
opts, register_counter, register_int_counter, register_int_gauge, Encoder, IntCounter,
89
IntGauge, TextEncoder, __register_gauge, register_int_counter_vec, IntCounterVec,
910
__register_counter_vec, histogram_opts, register_histogram_vec, HistogramVec,
1011
};
1112
use std::time::{Duration, Instant};
1213

13-
lazy_static::lazy_static! {
14-
static ref QUEUED_CRATES_COUNT: IntGauge = register_int_gauge!(
14+
static QUEUED_CRATES_COUNT: Lazy<IntGauge> = Lazy::new(|| {
15+
register_int_gauge!(
1516
"docsrs_queued_crates_count",
1617
"Number of crates in the build queue"
1718
)
18-
.unwrap();
19+
.unwrap()
20+
});
1921

20-
pub static ref PRIORITIZED_CRATES_COUNT: IntGauge = register_int_gauge!(
22+
pub static PRIORITIZED_CRATES_COUNT: Lazy<IntGauge> = Lazy::new(|| {
23+
register_int_gauge!(
2124
"docsrs_prioritized_crates_count",
2225
"Number of crates in the build queue that have a positive priority"
2326
)
24-
.unwrap();
27+
.unwrap()
28+
});
2529

26-
static ref FAILED_CRATES_COUNT: IntGauge = register_int_gauge!(
30+
static FAILED_CRATES_COUNT: Lazy<IntGauge> = Lazy::new(|| {
31+
register_int_gauge!(
2732
"docsrs_failed_crates_count",
2833
"Number of crates that failed to build"
2934
)
30-
.unwrap();
35+
.unwrap()
36+
});
3137

32-
pub static ref TOTAL_BUILDS: IntCounter = register_int_counter!(
33-
"docsrs_total_builds",
34-
"Number of crates built"
35-
)
36-
.unwrap();
38+
pub static TOTAL_BUILDS: Lazy<IntCounter> =
39+
Lazy::new(|| register_int_counter!("docsrs_total_builds", "Number of crates built").unwrap());
3740

38-
pub static ref SUCCESSFUL_BUILDS: IntCounter = register_int_counter!(
41+
pub static SUCCESSFUL_BUILDS: Lazy<IntCounter> = Lazy::new(|| {
42+
register_int_counter!(
3943
"docsrs_successful_builds",
4044
"Number of builds that successfully generated docs"
4145
)
42-
.unwrap();
46+
.unwrap()
47+
});
4348

44-
pub static ref FAILED_BUILDS: IntCounter = register_int_counter!(
49+
pub static FAILED_BUILDS: Lazy<IntCounter> = Lazy::new(|| {
50+
register_int_counter!(
4551
"docsrs_failed_builds",
4652
"Number of builds that generated a compile error"
4753
)
48-
.unwrap();
54+
.unwrap()
55+
});
4956

50-
pub static ref NON_LIBRARY_BUILDS: IntCounter = register_int_counter!(
57+
pub static NON_LIBRARY_BUILDS: Lazy<IntCounter> = Lazy::new(|| {
58+
register_int_counter!(
5159
"docsrs_non_library_builds",
5260
"Number of builds that did not complete due to not being a library"
5361
)
54-
.unwrap();
62+
.unwrap()
63+
});
5564

56-
pub static ref UPLOADED_FILES_TOTAL: IntCounter = register_int_counter!(
65+
pub static UPLOADED_FILES_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
66+
register_int_counter!(
5767
"docsrs_uploaded_files_total",
5868
"Number of files uploaded to S3 or stored in the database"
5969
)
60-
.unwrap();
70+
.unwrap()
71+
});
6172

62-
pub static ref ROUTES_VISITED: IntCounterVec = register_int_counter_vec!(
73+
pub static ROUTES_VISITED: Lazy<IntCounterVec> = Lazy::new(|| {
74+
register_int_counter_vec!(
6375
"docsrs_routes_visited",
6476
"The traffic of various docs.rs routes",
6577
&["route"]
6678
)
67-
.unwrap();
79+
.unwrap()
80+
});
6881

69-
pub static ref RESPONSE_TIMES: HistogramVec = register_histogram_vec!(
82+
pub static RESPONSE_TIMES: Lazy<HistogramVec> = Lazy::new(|| {
83+
register_histogram_vec!(
7084
"docsrs_response_time",
7185
"The response times of various docs.rs routes",
7286
&["route"]
7387
)
74-
.unwrap();
88+
.unwrap()
89+
});
7590

76-
pub static ref RUSTDOC_RENDERING_TIMES: HistogramVec = register_histogram_vec!(
91+
pub static RUSTDOC_RENDERING_TIMES: Lazy<HistogramVec> = Lazy::new(|| {
92+
register_histogram_vec!(
7793
"docsrs_rustdoc_rendering_time",
7894
"The time it takes to render a rustdoc page",
7995
&["step"]
8096
)
81-
.unwrap();
97+
.unwrap()
98+
});
8299

83-
pub static ref FAILED_DB_CONNECTIONS: IntCounter = register_int_counter!(
100+
pub static FAILED_DB_CONNECTIONS: Lazy<IntCounter> = Lazy::new(|| {
101+
register_int_counter!(
84102
"docsrs_failed_db_connections",
85103
"Number of attempted and failed connections to the database"
86104
)
87-
.unwrap();
105+
.unwrap()
106+
});
88107

89-
pub static ref USED_DB_CONNECTIONS: IntGauge = register_int_gauge!(
108+
pub static USED_DB_CONNECTIONS: Lazy<IntGauge> = Lazy::new(|| {
109+
register_int_gauge!(
90110
"docsrs_used_db_connections",
91111
"The number of used database connections"
92112
)
93-
.unwrap();
113+
.unwrap()
114+
});
94115

95-
pub static ref IDLE_DB_CONNECTIONS: IntGauge = register_int_gauge!(
116+
pub static IDLE_DB_CONNECTIONS: Lazy<IntGauge> = Lazy::new(|| {
117+
register_int_gauge!(
96118
"docsrs_idle_db_connections",
97119
"The number of idle database connections"
98120
)
99-
.unwrap();
121+
.unwrap()
122+
});
100123

101-
pub static ref MAX_DB_CONNECTIONS: IntGauge = register_int_gauge!(
124+
pub static MAX_DB_CONNECTIONS: Lazy<IntGauge> = Lazy::new(|| {
125+
register_int_gauge!(
102126
"docsrs_max_db_connections",
103127
"The maximum database connections"
104128
)
105-
.unwrap();
129+
.unwrap()
130+
});
106131

107-
pub static ref OPEN_FILE_DESCRIPTORS: IntGauge = register_int_gauge!(
132+
#[cfg(not(windows))]
133+
pub static OPEN_FILE_DESCRIPTORS: Lazy<IntGauge> = Lazy::new(|| {
134+
register_int_gauge!(
108135
"docsrs_open_file_descriptors",
109136
"The number of currently opened file descriptors"
110137
)
111-
.unwrap();
138+
.unwrap()
139+
});
112140

113-
pub static ref CURRENTLY_RUNNING_THREADS: IntGauge = register_int_gauge!(
141+
#[cfg(not(windows))]
142+
pub static CURRENTLY_RUNNING_THREADS: Lazy<IntGauge> = Lazy::new(|| {
143+
register_int_gauge!(
114144
"docsrs_running_threads",
115145
"The number of threads being used by docs.rs"
116146
)
117-
.unwrap();
118-
}
147+
.unwrap()
148+
});
119149

120150
pub fn metrics_handler(req: &mut Request) -> IronResult<Response> {
121151
let pool = extension!(req, Pool);
@@ -231,6 +261,7 @@ impl Drop for RenderingTimesRecorder {
231261
#[cfg(test)]
232262
mod tests {
233263
use crate::test::{assert_success, wrapper};
264+
use once_cell::sync::Lazy;
234265
use std::{
235266
collections::HashMap,
236267
sync::{
@@ -240,9 +271,8 @@ mod tests {
240271
};
241272

242273
static ROUTES_VISITED: AtomicUsize = AtomicUsize::new(0);
243-
lazy_static::lazy_static! {
244-
static ref RESPONSE_TIMES: Mutex<HashMap<String, usize>> = Mutex::new(HashMap::new());
245-
}
274+
static RESPONSE_TIMES: Lazy<Mutex<HashMap<String, usize>>> =
275+
Lazy::new(|| Mutex::new(HashMap::new()));
246276

247277
pub fn record_tests(route: &str) {
248278
ROUTES_VISITED.fetch_add(1, Ordering::SeqCst);

src/web/page/handlebars.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
use handlebars_iron::Template;
44
use iron::response::Response;
55
use iron::{status, IronResult, Set};
6+
use once_cell::sync::Lazy;
67
use serde::{
78
ser::{SerializeStruct, Serializer},
89
Serialize,
910
};
1011
use serde_json::Value;
1112
use std::collections::BTreeMap;
1213

13-
lazy_static::lazy_static! {
14-
static ref RUSTC_RESOURCE_SUFFIX: String = load_rustc_resource_suffix()
15-
.unwrap_or_else(|_| "???".into());
16-
}
14+
static RUSTC_RESOURCE_SUFFIX: Lazy<String> =
15+
Lazy::new(|| load_rustc_resource_suffix().unwrap_or_else(|_| "???".into()));
1716

1817
fn load_rustc_resource_suffix() -> Result<String, failure::Error> {
1918
// New instances of the configuration or the connection pool shouldn't be created inside the

src/web/page/templates.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn load_rustc_resource_suffix(conn: &Connection) -> Result<String> {
7777
"SELECT value FROM config WHERE name = 'rustc_version';",
7878
&[],
7979
)?;
80+
8081
if res.is_empty() {
8182
failure::bail!("missing rustc version");
8283
}

0 commit comments

Comments
 (0)