From ba665373b83e3fed60515d81c272e2fafa5ae281 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 13 Nov 2025 12:14:05 +0100 Subject: [PATCH] jobs/sync_to_sparse_index: Add Fastly CDN support --- src/config/server.rs | 5 +++++ src/tests/util/test_app.rs | 1 + src/worker/jobs/index/sync.rs | 25 ++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/config/server.rs b/src/config/server.rs index 254ac3c4d7b..65351e7716e 100644 --- a/src/config/server.rs +++ b/src/config/server.rs @@ -99,6 +99,9 @@ pub struct Server { /// Include publication timestamp in index entries (ISO8601 format). pub index_include_pubtime: bool, + + /// Enable Fastly CDN invalidation for sparse index files. + pub sparse_index_fastly_enabled: bool, } impl Server { @@ -248,6 +251,8 @@ impl Server { disable_token_creation, banner_message, index_include_pubtime, + sparse_index_fastly_enabled: var_parsed("SPARSE_INDEX_FASTLY_ENABLED")? + .unwrap_or(false), }) } } diff --git a/src/tests/util/test_app.rs b/src/tests/util/test_app.rs index 943c2d61997..ff1d31d88a3 100644 --- a/src/tests/util/test_app.rs +++ b/src/tests/util/test_app.rs @@ -524,6 +524,7 @@ fn simple_config() -> config::Server { disable_token_creation: None, banner_message: None, index_include_pubtime: false, + sparse_index_fastly_enabled: true, } } diff --git a/src/worker/jobs/index/sync.rs b/src/worker/jobs/index/sync.rs index 2d8c533d687..18be5de33ba 100644 --- a/src/worker/jobs/index/sync.rs +++ b/src/worker/jobs/index/sync.rs @@ -12,7 +12,7 @@ use std::fs::File; use std::io::{ErrorKind, Write}; use std::sync::Arc; use std::time::Instant; -use tracing::{debug, info, instrument}; +use tracing::{debug, info, instrument, warn}; #[derive(Serialize, Deserialize)] pub struct SyncToGitIndex { @@ -121,9 +121,28 @@ impl BackgroundJob for SyncToSparseIndex { let future = env.storage.sync_index(&self.krate, content); future.await.context("Failed to sync index data")?; - if env.cloudfront().is_some() { - let path = Repository::relative_index_file_for_url(&self.krate); + let path = Repository::relative_index_file_for_url(&self.krate); + + if let Some(fastly) = env.fastly() + && env.config.sparse_index_fastly_enabled + { + let domain_name = &env.config.domain_name; + let domains = [ + format!("index.{}", domain_name), + format!("fastly-index.{}", domain_name), + ]; + + for domain in domains { + if let Err(error) = fastly.purge(&domain, &path).await { + warn!( + domain, + path, "Failed to invalidate sparse index on Fastly: {error}" + ); + } + } + } + if env.cloudfront().is_some() { info!(%path, "Queuing index file invalidation on CloudFront"); let paths = &[path];