Skip to content

Commit

Permalink
fix: lazy initialize client (#1511)
Browse files Browse the repository at this point in the history
Fixes #1499 

The config from the CLI was set *after* the reqwest client was
initialized. I modified the initialization to create the client lazily.
This ensures that the config is properly set on the project before the
client is initialized.

Co-authored-by: Tim de Jager <[email protected]>
  • Loading branch information
baszalmstra and tdejager authored Jun 18, 2024
1 parent f8f84d3 commit d264c03
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ impl EnvironmentVars {
pub struct Project {
/// Root folder of the project
root: PathBuf,
/// Reqwest client shared for this project
client: reqwest::Client,
/// Authenticated reqwest client shared for this project
authenticated_client: ClientWithMiddleware,
/// Reqwest client shared for this project.
/// This is wrapped in a `OnceLock` to allow for lazy initialization.
client: OnceLock<(reqwest::Client, ClientWithMiddleware)>,
/// The repodata gateway to use for answering queries about repodata.
/// This is wrapped in a `OnceLock` to allow for lazy initialization.
repodata_gateway: OnceLock<Gateway>,
Expand Down Expand Up @@ -174,12 +173,9 @@ impl Project {

let config = Config::load(&root);

let (client, authenticated_client) = build_reqwest_clients(Some(&config));

Self {
root,
client,
authenticated_client,
client: Default::default(),
manifest,
env_vars,
mapping_source: Default::default(),
Expand Down Expand Up @@ -260,12 +256,9 @@ impl Project {
// Load the user configuration from the local project and all default locations
let config = Config::load(root);

let (client, authenticated_client) = build_reqwest_clients(Some(&config));

Ok(Self {
root: root.to_owned(),
client,
authenticated_client,
client: Default::default(),
manifest,
env_vars,
mapping_source: Default::default(),
Expand Down Expand Up @@ -571,13 +564,18 @@ impl Project {

/// Returns the reqwest client used for http networking
pub fn client(&self) -> &reqwest::Client {
&self.client
&self.client_and_authenticated_client().0
}

/// Create an authenticated reqwest client for this project
/// use authentication from `rattler_networking`
pub fn authenticated_client(&self) -> &ClientWithMiddleware {
&self.authenticated_client
&self.client_and_authenticated_client().1
}

fn client_and_authenticated_client(&self) -> &(reqwest::Client, ClientWithMiddleware) {
self.client
.get_or_init(|| build_reqwest_clients(Some(&self.config)))
}

pub fn config(&self) -> &Config {
Expand Down

0 comments on commit d264c03

Please sign in to comment.