Merge pull request #3925 from element-hq/quenting/background-metadata-warmup

Fetch the upstream OIDC metadata in the background on startup
This commit is contained in:
Quentin Gliech authored and GitHub committed 2025-01-31 11:32:29 +01:00
commit e315c2fdbf
2 files changed
+37 -23

No files matched your search

+36 -21
View File
@@ -25,6 +25,7 @@ use mas_templates::Templates;
use opentelemetry::{metrics::Histogram, KeyValue};
use rand::SeedableRng;
use sqlx::PgPool;
use tracing::Instrument;
use crate::telemetry::METER;
@@ -85,29 +86,43 @@ impl AppState {
self.conn_acquisition_histogram = Some(histogram);
}
/// Init the metadata cache.
///
/// # Panics
///
/// Panics if the metadata cache could not be initialized.
pub async fn init_metadata_cache(&self) {
// XXX: this panics because the error is annoying to propagate
let conn = self
.pool
.acquire()
.await
.expect("Failed to acquire a database connection");
/// Init the metadata cache in the background
pub fn init_metadata_cache(&self) {
let pool = self.pool.clone();
let metadata_cache = self.metadata_cache.clone();
let http_client = self.http_client.clone();
let mut repo = PgRepository::from_conn(conn);
tokio::spawn(
async move {
let conn = match pool.acquire().await {
Ok(conn) => conn,
Err(e) => {
tracing::error!(
error = &e as &dyn std::error::Error,
"Failed to acquire a database connection"
);
return;
}
};
self.metadata_cache
.warm_up_and_run(
&self.http_client,
std::time::Duration::from_secs(60 * 15),
&mut repo,
)
.await
.expect("Failed to warm up the metadata cache");
let mut repo = PgRepository::from_conn(conn);
if let Err(e) = metadata_cache
.warm_up_and_run(
&http_client,
std::time::Duration::from_secs(60 * 15),
&mut repo,
)
.await
{
tracing::error!(
error = &e as &dyn std::error::Error,
"Failed to warm up the metadata cache"
);
}
}
.instrument(tracing::info_span!("metadata_cache.background_warmup")),
);
}
}
+1 -2
View File
@@ -234,8 +234,7 @@ impl Options {
conn_acquisition_histogram: None,
};
s.init_metrics();
// XXX: this might panic
s.init_metadata_cache().await;
s.init_metadata_cache();
s
};