Skip to content

Commit e315c2f

Browse files
authored
Merge pull request #3925 from element-hq/quenting/background-metadata-warmup
Fetch the upstream OIDC metadata in the background on startup
2 parents 226684f + 453fdcf commit e315c2f

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

crates/cli/src/app_state.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use mas_templates::Templates;
2525
use opentelemetry::{metrics::Histogram, KeyValue};
2626
use rand::SeedableRng;
2727
use sqlx::PgPool;
28+
use tracing::Instrument;
2829

2930
use crate::telemetry::METER;
3031

@@ -85,29 +86,43 @@ impl AppState {
8586
self.conn_acquisition_histogram = Some(histogram);
8687
}
8788

88-
/// Init the metadata cache.
89-
///
90-
/// # Panics
91-
///
92-
/// Panics if the metadata cache could not be initialized.
93-
pub async fn init_metadata_cache(&self) {
94-
// XXX: this panics because the error is annoying to propagate
95-
let conn = self
96-
.pool
97-
.acquire()
98-
.await
99-
.expect("Failed to acquire a database connection");
100-
101-
let mut repo = PgRepository::from_conn(conn);
102-
103-
self.metadata_cache
104-
.warm_up_and_run(
105-
&self.http_client,
106-
std::time::Duration::from_secs(60 * 15),
107-
&mut repo,
108-
)
109-
.await
110-
.expect("Failed to warm up the metadata cache");
89+
/// Init the metadata cache in the background
90+
pub fn init_metadata_cache(&self) {
91+
let pool = self.pool.clone();
92+
let metadata_cache = self.metadata_cache.clone();
93+
let http_client = self.http_client.clone();
94+
95+
tokio::spawn(
96+
async move {
97+
let conn = match pool.acquire().await {
98+
Ok(conn) => conn,
99+
Err(e) => {
100+
tracing::error!(
101+
error = &e as &dyn std::error::Error,
102+
"Failed to acquire a database connection"
103+
);
104+
return;
105+
}
106+
};
107+
108+
let mut repo = PgRepository::from_conn(conn);
109+
110+
if let Err(e) = metadata_cache
111+
.warm_up_and_run(
112+
&http_client,
113+
std::time::Duration::from_secs(60 * 15),
114+
&mut repo,
115+
)
116+
.await
117+
{
118+
tracing::error!(
119+
error = &e as &dyn std::error::Error,
120+
"Failed to warm up the metadata cache"
121+
);
122+
}
123+
}
124+
.instrument(tracing::info_span!("metadata_cache.background_warmup")),
125+
);
111126
}
112127
}
113128

crates/cli/src/commands/server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ impl Options {
234234
conn_acquisition_histogram: None,
235235
};
236236
s.init_metrics();
237-
// XXX: this might panic
238-
s.init_metadata_cache().await;
237+
s.init_metadata_cache();
239238
s
240239
};
241240

0 commit comments

Comments
 (0)