Skip to content

Commit f027614

Browse files
committed
Compare site: cache master-branch Rust commits to improve performance
1 parent a77fce4 commit f027614

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

collector/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ pub struct MasterCommit {
265265
/// Note that this does not contain try commits today, so it should not be used
266266
/// to validate hashes or expand them generally speaking. This may also change
267267
/// in the future.
268-
pub async fn master_commits() -> Result<Vec<MasterCommit>, Box<dyn std::error::Error + Sync + Send>>
269-
{
268+
pub async fn master_commits() -> anyhow::Result<Vec<MasterCommit>> {
270269
let response = reqwest::get("https://triage.rust-lang.org/bors-commit-list").await?;
271270
Ok(response.json().await?)
272271
}

site/src/comparison.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ pub async fn handle_triage(
2525
log::info!("handle_triage({:?})", body);
2626
let start = body.start;
2727
let end = body.end;
28-
let master_commits = collector::master_commits()
28+
ctxt.update_master_commits()
2929
.await
3030
.map_err(|e| format!("error retrieving master commit list: {}", e))?;
31+
let master_commits = &ctxt.master_commits.load().commits;
3132

3233
let start_artifact = ctxt
3334
.artifact_id_for_bound(start.clone(), true)
@@ -95,20 +96,22 @@ pub async fn handle_compare(
9596
ctxt: &SiteCtxt,
9697
) -> api::ServerResult<api::comparison::Response> {
9798
log::info!("handle_compare({:?})", body);
98-
let master_commits = collector::master_commits()
99+
ctxt.update_master_commits()
99100
.await
100101
.map_err(|e| format!("error retrieving master commit list: {}", e))?;
102+
let master_commits = &ctxt.master_commits.load().commits;
103+
101104
let end = body.end;
102105
let comparison =
103-
compare_given_commits(body.start, end.clone(), body.stat, ctxt, &master_commits)
106+
compare_given_commits(body.start, end.clone(), body.stat, ctxt, master_commits)
104107
.await
105108
.map_err(|e| format!("error comparing commits: {}", e))?
106109
.ok_or_else(|| format!("could not find end commit for bound {:?}", end))?;
107110

108111
let conn = ctxt.conn().await;
109-
let prev = comparison.prev(&master_commits);
110-
let next = comparison.next(&master_commits);
111-
let is_contiguous = comparison.is_contiguous(&*conn, &master_commits).await;
112+
let prev = comparison.prev(master_commits);
113+
let next = comparison.next(master_commits);
114+
let is_contiguous = comparison.is_contiguous(&*conn, master_commits).await;
112115
let benchmark_map = conn.get_benchmarks().await;
113116

114117
let comparisons = comparison
@@ -455,7 +458,9 @@ pub async fn compare(
455458
stat: String,
456459
ctxt: &SiteCtxt,
457460
) -> Result<Option<Comparison>, BoxedError> {
458-
let master_commits = collector::master_commits().await?;
461+
ctxt.update_master_commits().await?;
462+
let master_commits = &ctxt.master_commits.load().commits;
463+
459464
compare_given_commits(start, end, stat, ctxt, &master_commits).await
460465
}
461466

site/src/load.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use std::fs;
44
use std::ops::RangeInclusive;
55
use std::path::Path;
66
use std::sync::Arc;
7+
use std::time::Instant;
78

89
use anyhow::Context;
910
use chrono::{Duration, Utc};
1011
use serde::{Deserialize, Serialize};
1112

1213
use crate::db;
13-
use collector::Bound;
14+
use collector::{Bound, MasterCommit};
1415
use database::Date;
1516

1617
use crate::api::github;
@@ -100,6 +101,23 @@ pub struct Config {
100101
pub keys: Keys,
101102
}
102103

104+
#[derive(Debug)]
105+
pub struct MasterCommitCache {
106+
pub commits: Vec<MasterCommit>,
107+
pub updated: Instant,
108+
}
109+
110+
impl MasterCommitCache {
111+
/// Download the master-branch Rust commit list
112+
async fn download() -> anyhow::Result<Self> {
113+
let commits = collector::master_commits().await?;
114+
Ok(Self {
115+
commits,
116+
updated: Instant::now(),
117+
})
118+
}
119+
}
120+
103121
/// Site context object that contains global data
104122
pub struct SiteCtxt {
105123
/// Site configuration
@@ -108,6 +126,8 @@ pub struct SiteCtxt {
108126
pub landing_page: ArcSwap<Option<Arc<crate::api::graphs::Response>>>,
109127
/// Index of various common queries
110128
pub index: ArcSwap<crate::db::Index>,
129+
/// Cached master-branch Rust commits
130+
pub master_commits: ArcSwap<MasterCommitCache>,
111131
/// Database connection pool
112132
pub pool: Pool,
113133
}
@@ -160,9 +180,12 @@ impl SiteCtxt {
160180
}
161181
};
162182

183+
let master_commits = MasterCommitCache::download().await?;
184+
163185
Ok(Self {
164186
config,
165187
index: ArcSwap::new(Arc::new(index)),
188+
master_commits: ArcSwap::new(Arc::new(master_commits)),
166189
pool,
167190
landing_page: ArcSwap::new(Arc::new(None)),
168191
})
@@ -199,6 +222,16 @@ impl SiteCtxt {
199222
all_commits,
200223
)
201224
}
225+
226+
/// Download master-branch Rust commits if the cached value is older than one minute
227+
pub async fn update_master_commits(&self) -> anyhow::Result<()> {
228+
let commits = self.master_commits.load();
229+
if commits.updated.elapsed() > std::time::Duration::from_secs(60) {
230+
self.master_commits
231+
.store(Arc::new(MasterCommitCache::download().await?))
232+
}
233+
Ok(())
234+
}
202235
}
203236

204237
/// Calculating the missing commits.

site/src/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ impl Server {
277277
// Refresh the landing page
278278
ctxt.landing_page.store(Arc::new(None));
279279

280+
let _ = ctxt.update_master_commits().await;
281+
280282
// Spawn off a task to post the results of any commit results that we
281283
// are now aware of.
282284
tokio::spawn(async move {

0 commit comments

Comments
 (0)