client/push: Correctly delegate API requests based on cache config

This commit is contained in:
Zhaofeng Li 2023-01-08 00:57:22 -07:00
parent 05a5e9cca8
commit d547c86f08
2 changed files with 14 additions and 3 deletions

View file

@ -62,6 +62,12 @@ impl ApiClient {
}) })
} }
/// Sets the API endpoint of this client.
pub fn set_endpoint(&mut self, endpoint: &str) -> Result<()> {
self.endpoint = Url::parse(endpoint)?;
Ok(())
}
/// Returns the configuration of a cache. /// Returns the configuration of a cache.
pub async fn get_cache_config(&self, cache: &CacheName) -> Result<CacheConfig> { pub async fn get_cache_config(&self, cache: &CacheName) -> Result<CacheConfig> {
let endpoint = self let endpoint = self

View file

@ -194,10 +194,10 @@ pub async fn run(opts: Opts) -> Result<()> {
let (server_name, server, cache) = config.resolve_cache(&sub.cache)?; let (server_name, server, cache) = config.resolve_cache(&sub.cache)?;
let api = ApiClient::from_server_config(server.clone())?; let mut api = ApiClient::from_server_config(server.clone())?;
let plan = PushPlan::plan( let plan = PushPlan::plan(
store.clone(), store.clone(),
&api, &mut api,
cache, cache,
roots, roots,
sub.no_closure, sub.no_closure,
@ -261,7 +261,7 @@ impl PushPlan {
/// Creates a plan. /// Creates a plan.
async fn plan( async fn plan(
store: Arc<NixStore>, store: Arc<NixStore>,
api: &ApiClient, api: &mut ApiClient,
cache: &CacheName, cache: &CacheName,
roots: Vec<StorePath>, roots: Vec<StorePath>,
no_closure: bool, no_closure: bool,
@ -307,6 +307,11 @@ impl PushPlan {
// Confirm remote cache validity, query cache config // Confirm remote cache validity, query cache config
let cache_config = api.get_cache_config(cache).await?; let cache_config = api.get_cache_config(cache).await?;
if let Some(api_endpoint) = &cache_config.api_endpoint {
// Use delegated API endpoint
api.set_endpoint(api_endpoint)?;
}
if !ignore_upstream_filter { if !ignore_upstream_filter {
// Filter out paths signed by upstream caches // Filter out paths signed by upstream caches
let upstream_cache_key_names = let upstream_cache_key_names =