Skip to content

Commit

Permalink
Update dependency moka to v0.12.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuya6502 committed Oct 10, 2023
1 parent 177231e commit 98bd86c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 116 deletions.
116 changes: 6 additions & 110 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ipnetwork = "=0.20.0"
tikv-jemallocator = { version = "=0.5.4", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
lettre = { version = "=0.10.4", default-features = false, features = ["file-transport", "smtp-transport", "native-tls", "hostname", "builder"] }
minijinja = "=1.0.8"
moka = { version = "=0.11.3", features = ["future"] }
moka = { version = "=0.12.1", features = ["future"] }
oauth2 = { version = "=4.4.2", default-features = false, features = ["reqwest"] }
object_store = { version = "=0.7.1", features = ["aws"] }
once_cell = "=1.18.0"
Expand Down
18 changes: 13 additions & 5 deletions src/controllers/version/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::models::{Crate, VersionDownload};
use crate::schema::*;
use crate::views::EncodableVersionDownload;
use chrono::{Duration, NaiveDate, Utc};
use tracing::Instrument;

/// Handles the `GET /crates/:crate_id/:version/download` route.
/// This returns a URL to the location where the crate is stored.
Expand All @@ -22,8 +23,11 @@ pub async fn download(

let cache_key = (crate_name.to_string(), version.to_string());

let cache_result =
info_span!("cache.read", ?cache_key).in_scope(|| app.version_id_cacher.get(&cache_key));
let cache_result = app
.version_id_cacher
.get(&cache_key)
.instrument(info_span!("cache.read", ?cache_key))
.await;

let (crate_name, version) = if let Some(version_id) = cache_result {
app.instance_metrics.version_id_cache_hits.inc();
Expand All @@ -37,6 +41,7 @@ pub async fn download(
app.instance_metrics.version_id_cache_misses.inc();

let app = app.clone();
let rt = tokio::runtime::Handle::current();
conduit_compat(move || {
// When no database connection is ready unconditional redirects will be performed. This could
// happen if the pool is not healthy or if an operator manually configured the application to
Expand Down Expand Up @@ -89,9 +94,12 @@ pub async fn download(
// Non-canonical requests fallback to the "slow" path with a DB query, but
// we typically only get a few hundred non-canonical requests in a day anyway.
info_span!("cache.write", ?cache_key).in_scope(|| {
app.version_id_cacher
.blocking()
.insert(cache_key, version_id)
// SAFETY: This block_on should not panic. block_on will panic if the
// current thread is an executor thread of a Tokio runtime. (Will panic
// by "Cannot start a runtime from within a runtime"). Here, we are in
// a spawn_blocking call because of conduit_compat, so our current thread
// is not an executor of the runtime.
rt.block_on(app.version_id_cacher.insert(cache_key, version_id))
});

Ok((crate_name, version))
Expand Down

0 comments on commit 98bd86c

Please sign in to comment.