Skip to content

response_cache: automatically connect the cache storage when available #7911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ mod tests {
.await
.unwrap();
let storage = Arc::new(Storage {
all: Some(pg_cache),
all: Some(Arc::new(pg_cache.into())),
subgraphs: HashMap::new(),
});
let invalidation = Invalidation::new(storage.clone()).await.unwrap();
Expand Down Expand Up @@ -363,7 +363,7 @@ mod tests {
.await
.unwrap();
let storage = Arc::new(Storage {
all: Some(pg_cache),
all: Some(Arc::new(pg_cache.into())),
subgraphs: HashMap::new(),
});
let invalidation = Invalidation::new(storage.clone()).await.unwrap();
Expand Down
26 changes: 18 additions & 8 deletions apollo-router/src/plugins/response_cache/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use opentelemetry::KeyValue;
use opentelemetry::metrics::MeterProvider;
use parking_lot::Mutex;
use serde_json_bytes::Value;
use tokio::sync::broadcast;
use tokio_stream::StreamExt;
use tokio_stream::wrappers::IntervalStream;
use tower::BoxError;
Expand Down Expand Up @@ -306,6 +307,7 @@ impl From<CacheMetricContextKey> for String {
/// parameter subgraph_name is optional and is None when the database is the global one, and Some(...) when it's a database configured for a specific subgraph
pub(super) async fn expired_data_task(
pg_cache: PostgresCacheStorage,
mut abort_signal: broadcast::Receiver<()>,
subgraph_name: Option<String>,
) {
let mut interval = IntervalStream::new(tokio::time::interval(std::time::Duration::from_secs(
Expand Down Expand Up @@ -335,14 +337,22 @@ pub(super) async fn expired_data_task(
})
.init();

while (interval.next().await).is_some() {
let exp_data = match pg_cache.expired_data_count().await {
Ok(exp_data) => exp_data,
Err(err) => {
::tracing::error!(error = ?err, "cannot get expired data count");
continue;
loop {
tokio::select! {
biased;
_ = abort_signal.recv() => {
break;
}
};
expired_data_count.store(exp_data, Ordering::Relaxed);
_ = interval.next() => {
let exp_data = match pg_cache.expired_data_count().await {
Ok(exp_data) => exp_data,
Err(err) => {
::tracing::error!(error = ?err, "cannot get expired data count");
continue;
}
};
expired_data_count.store(exp_data, Ordering::Relaxed);
}
}
}
}
Loading