Skip to content

Commit f9e2344

Browse files
committed
feat: Recover from index connection failures
If there exists an index cache entry for the requested crate, recover from crates.io index connection failures by serving a possibly stale index entry from the local cache. Log a warning when a possibly stale index cache entry is served.
1 parent 366412a commit f9e2344

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,25 @@ fn forward_index_request(
401401
send_error_response(request, 503);
402402
}
403403
}
404-
Err(err) => send_fetch_error_response(request, err),
404+
Err(err) => {
405+
if let ureq::Error::Transport(err) = err.as_ref() {
406+
if let Some(data) = cache_fetch_index_entry(&config.index_dir, &entry) {
407+
error!("fetch: index connection failed: {err}");
408+
409+
// The upstream registry can not be reached at the moment, likely
410+
// due to an intermittent network failure.
411+
// Serve a possibly stale index entry file from the local filesystem
412+
// cache anyway to keep the clients running.
413+
warn!("proxy: forwarding possibly stale cached index data for {entry}");
414+
415+
send_index_entry_file_response(request, entry, data);
416+
return;
417+
}
418+
}
419+
420+
// Forward non-recoverable download errors back to the clients.
421+
send_fetch_error_response(request, err);
422+
}
405423
};
406424

407425
std::thread::Builder::new()

0 commit comments

Comments
 (0)