Skip to content

Commit 81a8398

Browse files
committed
make Context.async_storage an async function
1 parent 62c620e commit 81a8398

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

src/bin/cratesfyi.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::str::FromStr;
66
use std::sync::Arc;
77

88
use anyhow::{anyhow, Context as _, Error, Result};
9+
use axum::async_trait;
910
use clap::{Parser, Subcommand, ValueEnum};
1011
use docs_rs::cdn::CdnBackend;
1112
use docs_rs::db::{self, add_path_into_database, Overrides, Pool, PoolClient};
@@ -767,6 +768,7 @@ macro_rules! lazy {
767768
}
768769
}
769770

771+
#[async_trait]
770772
impl Context for BinContext {
771773
lazy! {
772774
fn build_queue(self) -> BuildQueue = BuildQueue::new(
@@ -775,10 +777,13 @@ impl Context for BinContext {
775777
self.config()?,
776778
self.storage()?,
777779
);
778-
fn storage(self) -> Storage = Storage::new(
779-
self.async_storage()?,
780-
self.runtime()?
781-
);
780+
fn storage(self) -> Storage = {
781+
let runtime = self.runtime()?;
782+
Storage::new(
783+
runtime.block_on(self.async_storage())?,
784+
runtime
785+
)
786+
};
782787
fn cdn(self) -> CdnBackend = CdnBackend::new(
783788
&self.config()?,
784789
&self.runtime()?,
@@ -824,11 +829,9 @@ impl Context for BinContext {
824829
.clone())
825830
}
826831

827-
fn async_storage(&self) -> Result<Arc<AsyncStorage>> {
828-
Ok(Arc::new(self.runtime()?.block_on(AsyncStorage::new(
829-
self.pool()?,
830-
self.instance_metrics()?,
831-
self.config()?,
832-
))?))
832+
async fn async_storage(&self) -> Result<Arc<AsyncStorage>> {
833+
Ok(Arc::new(
834+
AsyncStorage::new(self.pool()?, self.instance_metrics()?, self.config()?).await?,
835+
))
833836
}
834837
}

src/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use crate::repositories::RepositoryStatsUpdater;
55
use crate::{
66
AsyncStorage, BuildQueue, Config, Index, InstanceMetrics, RegistryApi, ServiceMetrics, Storage,
77
};
8+
use axum::async_trait;
89
use std::sync::Arc;
910
use tokio::runtime::Runtime;
1011

12+
#[async_trait]
1113
pub trait Context {
1214
fn config(&self) -> Result<Arc<Config>>;
1315
fn build_queue(&self) -> Result<Arc<BuildQueue>>;
1416
fn storage(&self) -> Result<Arc<Storage>>;
15-
fn async_storage(&self) -> Result<Arc<AsyncStorage>>;
17+
async fn async_storage(&self) -> Result<Arc<AsyncStorage>>;
1618
fn cdn(&self) -> Result<Arc<CdnBackend>>;
1719
fn pool(&self) -> Result<Pool>;
1820
fn service_metrics(&self) -> Result<Arc<ServiceMetrics>>;

src/test/mod.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ use crate::storage::{AsyncStorage, Storage, StorageKind};
99
use crate::web::{build_axum_app, cache, page::TemplateData};
1010
use crate::{BuildQueue, Config, Context, Index, InstanceMetrics, RegistryApi, ServiceMetrics};
1111
use anyhow::Context as _;
12+
use axum::async_trait;
1213
use fn_error_context::context;
1314
use futures_util::FutureExt;
14-
use once_cell::unsync::OnceCell;
15+
use once_cell::sync::OnceCell;
1516
use postgres::Client as Connection;
1617
use reqwest::{
1718
blocking::{Client, ClientBuilder, RequestBuilder, Response},
@@ -263,7 +264,7 @@ pub(crate) struct TestEnvironment {
263264
config: OnceCell<Arc<Config>>,
264265
db: tokio::sync::OnceCell<TestDatabase>,
265266
storage: OnceCell<Arc<Storage>>,
266-
async_storage: OnceCell<Arc<AsyncStorage>>,
267+
async_storage: tokio::sync::OnceCell<Arc<AsyncStorage>>,
267268
cdn: OnceCell<Arc<CdnBackend>>,
268269
index: OnceCell<Arc<Index>>,
269270
registry_api: OnceCell<Arc<RegistryApi>>,
@@ -298,7 +299,7 @@ impl TestEnvironment {
298299
config: OnceCell::new(),
299300
db: tokio::sync::OnceCell::new(),
300301
storage: OnceCell::new(),
301-
async_storage: OnceCell::new(),
302+
async_storage: tokio::sync::OnceCell::new(),
302303
cdn: OnceCell::new(),
303304
index: OnceCell::new(),
304305
registry_api: OnceCell::new(),
@@ -391,25 +392,29 @@ impl TestEnvironment {
391392
.clone()
392393
}
393394

394-
pub(crate) fn async_storage(&self) -> Arc<AsyncStorage> {
395+
pub(crate) async fn async_storage(&self) -> Arc<AsyncStorage> {
395396
self.async_storage
396-
.get_or_init(|| {
397+
.get_or_init(|| async {
398+
let db = self.async_db().await;
397399
Arc::new(
398-
self.runtime()
399-
.block_on(AsyncStorage::new(
400-
self.db().pool(),
401-
self.instance_metrics(),
402-
self.config(),
403-
))
400+
AsyncStorage::new(db.pool(), self.instance_metrics(), self.config())
401+
.await
404402
.expect("failed to initialize the async storage"),
405403
)
406404
})
405+
.await
407406
.clone()
408407
}
409408

410409
pub(crate) fn storage(&self) -> Arc<Storage> {
410+
let runtime = self.runtime();
411411
self.storage
412-
.get_or_init(|| Arc::new(Storage::new(self.async_storage(), self.runtime())))
412+
.get_or_init(|| {
413+
Arc::new(Storage::new(
414+
runtime.block_on(self.async_storage()),
415+
runtime,
416+
))
417+
})
413418
.clone()
414419
}
415420

@@ -515,6 +520,7 @@ impl TestEnvironment {
515520
}
516521
}
517522

523+
#[async_trait]
518524
impl Context for TestEnvironment {
519525
fn config(&self) -> Result<Arc<Config>> {
520526
Ok(TestEnvironment::config(self))
@@ -528,8 +534,8 @@ impl Context for TestEnvironment {
528534
Ok(TestEnvironment::storage(self))
529535
}
530536

531-
fn async_storage(&self) -> Result<Arc<AsyncStorage>> {
532-
Ok(TestEnvironment::async_storage(self))
537+
async fn async_storage(&self) -> Result<Arc<AsyncStorage>> {
538+
Ok(TestEnvironment::async_storage(self).await)
533539
}
534540

535541
fn cdn(&self) -> Result<Arc<CdnBackend>> {

src/web/crate_details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ mod tests {
15331533
.unwrap();
15341534
assert!(matches!(
15351535
env.runtime()
1536-
.block_on(details.fetch_readme(&env.async_storage())),
1536+
.block_on(details.fetch_readme(&env.runtime().block_on(env.async_storage()))),
15371537
Ok(None)
15381538
));
15391539
Ok(())

src/web/file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mod tests {
7878
let mut file = env
7979
.runtime()
8080
.block_on(File::from_path(
81-
&env.async_storage(),
81+
&env.runtime().block_on(env.async_storage()),
8282
"rustdoc/fake-package/1.0.0/fake-package/index.html",
8383
&env.config(),
8484
))
@@ -125,7 +125,7 @@ mod tests {
125125

126126
let file = |path| {
127127
env.runtime().block_on(File::from_path(
128-
&env.async_storage(),
128+
&env.runtime().block_on(env.async_storage()),
129129
&format!("rustdoc/dummy/0.1.0/{path}"),
130130
&env.config(),
131131
))

src/web/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ fn apply_middleware(
275275
) -> Result<AxumRouter> {
276276
let config = context.config()?;
277277
let has_templates = template_data.is_some();
278+
let async_storage = context.runtime()?.block_on(context.async_storage())?;
278279
Ok(router.layer(
279280
ServiceBuilder::new()
280281
.layer(TraceLayer::new_for_http())
@@ -293,7 +294,7 @@ fn apply_middleware(
293294
.layer(Extension(context.instance_metrics()?))
294295
.layer(Extension(context.config()?))
295296
.layer(Extension(context.storage()?))
296-
.layer(Extension(context.async_storage()?))
297+
.layer(Extension(async_storage))
297298
.layer(Extension(context.repository_stats_updater()?))
298299
.layer(option_layer(template_data.map(Extension)))
299300
.layer(middleware::from_fn(csp::csp_middleware))

0 commit comments

Comments
 (0)