Skip to content
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
5 changes: 5 additions & 0 deletions src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/util/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
25 changes: 22 additions & 3 deletions src/worker/jobs/index/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand Down