Skip to content

Commit 65b5734

Browse files
committed
fix archive index caching for source archives, don't ignore source fetch errors
1 parent 87e09aa commit 65b5734

5 files changed

+118
-36
lines changed

.sqlx/query-73bf03e72a4795b54f878d30c30d2895a2acfa491b5925b1e576de5e6b686b8b.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

.sqlx/query-8fdb5774a13ea9d9a70552395a4b275e859e91e439ddcd3f6be9e4cfa845f94d.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/storage/mod.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,37 @@ impl AsyncStorage {
201201
})
202202
}
203203

204+
pub(crate) async fn source_file_exists(
205+
&self,
206+
name: &str,
207+
version: &str,
208+
latest_build_id: i32,
209+
path: &str,
210+
archive_storage: bool,
211+
) -> Result<bool> {
212+
Ok(if archive_storage {
213+
self.exists_in_archive(&source_archive_path(name, version), latest_build_id, path)
214+
.await?
215+
} else {
216+
// Add sources prefix, name and version to the path for accessing the file stored in the database
217+
let remote_path = format!("sources/{name}/{version}/{path}");
218+
self.exists(&remote_path).await?
219+
})
220+
}
221+
204222
#[context("fetching {path} from {name} {version} (archive: {archive_storage})")]
205223
pub(crate) async fn fetch_source_file(
206224
&self,
207225
name: &str,
208226
version: &str,
227+
latest_build_id: i32,
209228
path: &str,
210229
archive_storage: bool,
211230
) -> Result<Blob> {
212231
Ok(if archive_storage {
213232
self.get_from_archive(
214233
&source_archive_path(name, version),
215-
0, // we assume the source archive can't ever change for a release
234+
latest_build_id,
216235
path,
217236
self.max_file_size_for(path),
218237
None,
@@ -633,13 +652,34 @@ impl Storage {
633652
&self,
634653
name: &str,
635654
version: &str,
655+
latest_build_id: i32,
636656
path: &str,
637657
archive_storage: bool,
638658
) -> Result<Blob> {
639-
self.runtime.block_on(
640-
self.inner
641-
.fetch_source_file(name, version, path, archive_storage),
642-
)
659+
self.runtime.block_on(self.inner.fetch_source_file(
660+
name,
661+
version,
662+
latest_build_id,
663+
path,
664+
archive_storage,
665+
))
666+
}
667+
668+
pub(crate) fn source_file_exists(
669+
&self,
670+
name: &str,
671+
version: &str,
672+
latest_build_id: i32,
673+
path: &str,
674+
archive_storage: bool,
675+
) -> Result<bool> {
676+
self.runtime.block_on(self.inner.source_file_exists(
677+
name,
678+
version,
679+
latest_build_id,
680+
path,
681+
archive_storage,
682+
))
643683
}
644684

645685
pub(crate) fn rustdoc_file_exists(

src/web/crate_details.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl CrateDetails {
257257
.fetch_source_file(
258258
&self.name,
259259
&self.version,
260+
self.latest_build_id.unwrap_or(0),
260261
"Cargo.toml",
261262
self.archive_storage,
262263
)
@@ -282,7 +283,13 @@ impl CrateDetails {
282283
};
283284
for path in &paths {
284285
match storage
285-
.fetch_source_file(&self.name, &self.version, path, self.archive_storage)
286+
.fetch_source_file(
287+
&self.name,
288+
&self.version,
289+
self.latest_build_id.unwrap_or(0),
290+
path,
291+
self.archive_storage,
292+
)
286293
.await
287294
{
288295
Ok(readme) => {

src/web/source.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
},
1010
AsyncStorage,
1111
};
12-
use anyhow::Result;
12+
use anyhow::{Context as _, Result};
1313
use axum::{extract::Path, headers::HeaderMapExt, response::IntoResponse, Extension};
1414
use serde::{Deserialize, Serialize};
1515
use std::{cmp::Ordering, sync::Arc};
@@ -225,8 +225,18 @@ pub(crate) async fn source_browser_handler(
225225
}
226226
};
227227

228-
let archive_storage = sqlx::query_scalar!(
229-
"SELECT archive_storage
228+
let row = sqlx::query!(
229+
"SELECT
230+
releases.archive_storage,
231+
(
232+
SELECT id
233+
FROM builds
234+
WHERE
235+
builds.rid = releases.id AND
236+
builds.build_status = TRUE
237+
ORDER BY build_time DESC
238+
LIMIT 1
239+
) AS latest_build_id
230240
FROM releases
231241
INNER JOIN crates ON releases.crate_id = crates.id
232242
WHERE
@@ -240,11 +250,30 @@ pub(crate) async fn source_browser_handler(
240250

241251
// try to get actual file first
242252
// skip if request is a directory
243-
let blob = if !path.ends_with('/') {
244-
storage
245-
.fetch_source_file(&name, &version, &path, archive_storage)
253+
let blob = if !path.ends_with('/')
254+
&& storage
255+
.source_file_exists(
256+
&name,
257+
&version,
258+
row.latest_build_id.unwrap_or(0),
259+
&path,
260+
row.archive_storage,
261+
)
246262
.await
247-
.ok()
263+
.context("error checking source file existence")?
264+
{
265+
Some(
266+
storage
267+
.fetch_source_file(
268+
&name,
269+
&version,
270+
row.latest_build_id.unwrap_or(0),
271+
&path,
272+
row.archive_storage,
273+
)
274+
.await
275+
.context("error fetching source file")?,
276+
)
248277
} else {
249278
None
250279
};

0 commit comments

Comments
 (0)