Skip to content

Commit d06c73e

Browse files
committed
Use dashmap to record recent release access to avoid locking
1 parent 1fa9854 commit d06c73e

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

Cargo.lock

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ once_cell = { version = "1.4.0", features = ["parking_lot"] }
4545
base64 = "0.12.1"
4646
strum = { version = "0.18.0", features = ["derive"] }
4747
lol_html = "0.2"
48+
dashmap = "3.11.10"
4849

4950
# Async
5051
tokio = { version = "0.2.22", features = ["rt-threaded"] }

src/metrics/mod.rs

+12-32
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ mod macros;
44
use self::macros::MetricFromOpts;
55
use crate::db::Pool;
66
use crate::BuildQueue;
7+
use dashmap::DashMap;
78
use failure::Error;
89
use prometheus::proto::MetricFamily;
9-
use std::{
10-
collections::HashMap,
11-
sync::Mutex,
12-
time::{Duration, Instant},
13-
};
10+
use std::time::{Duration, Instant};
1411

1512
load_metric_type!(IntGauge as single);
1613
load_metric_type!(IntCounter as single);
@@ -80,39 +77,28 @@ metrics! {
8077
namespace: "docsrs",
8178
}
8279

83-
#[derive(Debug)]
80+
#[derive(Debug, Default)]
8481
pub(crate) struct RecentReleases {
85-
krates: Mutex<HashMap<String, Instant>>,
86-
versions: Mutex<HashMap<String, Instant>>,
87-
platforms: Mutex<HashMap<String, Instant>>,
82+
krates: DashMap<String, Instant>,
83+
versions: DashMap<String, Instant>,
84+
platforms: DashMap<String, Instant>,
8885
}
8986

9087
impl RecentReleases {
9188
pub(crate) fn new() -> Self {
92-
Self {
93-
krates: Mutex::new(HashMap::new()),
94-
versions: Mutex::new(HashMap::new()),
95-
platforms: Mutex::new(HashMap::new()),
96-
}
89+
Self::default()
9790
}
9891

9992
pub(crate) fn record(&self, krate: &str, version: &str, target: &str) {
100-
self.krates
101-
.lock()
102-
.unwrap()
103-
.insert(krate.to_owned(), Instant::now());
93+
self.krates.insert(krate.to_owned(), Instant::now());
10494
self.versions
105-
.lock()
106-
.unwrap()
10795
.insert(format!("{}/{}", krate, version), Instant::now());
10896
self.platforms
109-
.lock()
110-
.unwrap()
11197
.insert(format!("{}/{}/{}", krate, version, target), Instant::now());
11298
}
11399

114100
pub(crate) fn gather(&self, metrics: &Metrics) {
115-
fn inner(map: &mut HashMap<String, Instant>, metric: &IntGaugeVec) {
101+
fn inner(map: &DashMap<String, Instant>, metric: &IntGaugeVec) {
116102
let mut hour_count = 0;
117103
let mut half_hour_count = 0;
118104
let mut five_minute_count = 0;
@@ -144,15 +130,9 @@ impl RecentReleases {
144130
.set(five_minute_count);
145131
}
146132

147-
inner(&mut *self.krates.lock().unwrap(), &metrics.recent_krates);
148-
inner(
149-
&mut *self.versions.lock().unwrap(),
150-
&metrics.recent_versions,
151-
);
152-
inner(
153-
&mut *self.platforms.lock().unwrap(),
154-
&metrics.recent_platforms,
155-
);
133+
inner(&self.krates, &metrics.recent_krates);
134+
inner(&self.versions, &metrics.recent_versions);
135+
inner(&self.platforms, &metrics.recent_platforms);
156136
}
157137
}
158138

0 commit comments

Comments
 (0)