Skip to content

Commit

Permalink
fix: non-global reqwest client (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdejager authored Jan 22, 2024
1 parent de5cb03 commit d5f2adb
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 63 deletions.
14 changes: 8 additions & 6 deletions src/cli/global/install.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::install::execute_transaction;
use crate::repodata::friendly_channel_name;
use crate::{
config, default_authenticated_client, prefix::Prefix, progress::await_in_progress,
repodata::fetch_sparse_repodata,
};
use crate::{config, prefix::Prefix, progress::await_in_progress, repodata::fetch_sparse_repodata};
use clap::Parser;
use dirs::home_dir;
use itertools::Itertools;
use miette::IntoDiagnostic;
use rattler::install::Transaction;
use rattler_conda_types::{Channel, ChannelConfig, MatchSpec, PackageName, Platform, PrefixRecord};
use rattler_networking::AuthenticatedClient;
use rattler_repodata_gateway::sparse::SparseRepoData;
use rattler_shell::{
activation::{ActivationVariables, Activator, PathModificationBehavior},
Expand Down Expand Up @@ -330,18 +328,21 @@ pub async fn execute(args: Args) -> miette::Result<()> {
.map(|c| Channel::from_str(c, &channel_config))
.collect::<Result<Vec<Channel>, _>>()
.into_diagnostic()?;
let authenticated_client = AuthenticatedClient::default();

// Find the MatchSpec we want to install
let package_matchspec = MatchSpec::from_str(&args.package).into_diagnostic()?;

// Fetch sparse repodata
let platform_sparse_repodata = fetch_sparse_repodata(&channels, [Platform::current()]).await?;
let platform_sparse_repodata =
fetch_sparse_repodata(&channels, [Platform::current()], &authenticated_client).await?;

// Install the package
let (prefix_package, scripts, _) = globally_install_package(
package_matchspec,
&platform_sparse_repodata,
&channel_config,
authenticated_client,
)
.await?;

Expand Down Expand Up @@ -394,6 +395,7 @@ pub(super) async fn globally_install_package(
package_matchspec: MatchSpec,
platform_sparse_repodata: &[SparseRepoData],
channel_config: &ChannelConfig,
authenticated_client: AuthenticatedClient,
) -> miette::Result<(PrefixRecord, Vec<PathBuf>, bool)> {
let package_name = package_name(&package_matchspec)?;

Expand Down Expand Up @@ -449,7 +451,7 @@ pub(super) async fn globally_install_package(
&prefix_records,
prefix.root().to_path_buf(),
config::get_cache_dir()?,
default_authenticated_client(),
authenticated_client,
),
)
.await?;
Expand Down
6 changes: 5 additions & 1 deletion src/cli/global/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;
use clap::Parser;
use miette::IntoDiagnostic;
use rattler_conda_types::{Channel, ChannelConfig, MatchSpec, Platform};
use rattler_networking::AuthenticatedClient;

use crate::repodata::fetch_sparse_repodata;

Expand Down Expand Up @@ -53,14 +54,17 @@ pub async fn execute(args: Args) -> miette::Result<()> {
);
}

let authenticated_client = AuthenticatedClient::default();
// Fetch sparse repodata
let platform_sparse_repodata = fetch_sparse_repodata(&channels, [Platform::current()]).await?;
let platform_sparse_repodata =
fetch_sparse_repodata(&channels, [Platform::current()], &authenticated_client).await?;

// Install the package
let (package_record, _, upgraded) = globally_install_package(
package_matchspec,
&platform_sparse_repodata,
&channel_config,
authenticated_client,
)
.await?;

Expand Down
12 changes: 10 additions & 2 deletions src/cli/global/upgrade_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use futures::{stream, StreamExt, TryStreamExt};
use miette::IntoDiagnostic;
use rattler_conda_types::{Channel, ChannelConfig, Platform};
use rattler_networking::AuthenticatedClient;

use crate::repodata::fetch_sparse_repodata;

Expand Down Expand Up @@ -36,8 +37,10 @@ pub async fn execute(args: Args) -> miette::Result<()> {

let packages = list_global_packages().await?;

let authenticated_client = AuthenticatedClient::default();
// Fetch sparse repodata
let platform_sparse_repodata = fetch_sparse_repodata(&channels, [Platform::current()]).await?;
let platform_sparse_repodata =
fetch_sparse_repodata(&channels, [Platform::current()], &authenticated_client).await?;

let tasks = packages
.iter()
Expand All @@ -47,7 +50,12 @@ pub async fn execute(args: Args) -> miette::Result<()> {

let task_stream = stream::iter(tasks)
.map(|matchspec| {
globally_install_package(matchspec, &platform_sparse_repodata, &channel_config)
globally_install_package(
matchspec,
&platform_sparse_repodata,
&channel_config,
authenticated_client.clone(),
)
})
.buffered(UPGRADE_ALL_CONCURRENCY);

Expand Down
11 changes: 9 additions & 2 deletions src/cli/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use clap::Parser;
use itertools::Itertools;
use miette::IntoDiagnostic;
use rattler_conda_types::{Channel, ChannelConfig, PackageName, Platform, RepoDataRecord};
use rattler_networking::AuthenticatedClient;
use rattler_repodata_gateway::sparse::SparseRepoData;
use regex::Regex;

use strsim::jaro;
use tokio::task::spawn_blocking;

Expand Down Expand Up @@ -99,8 +101,13 @@ pub async fn execute(args: Args) -> miette::Result<()> {
};

let package_name_filter = args.package;
let repo_data =
fetch_sparse_repodata(channels.iter().map(AsRef::as_ref), [Platform::current()]).await?;
let authenticated_client = AuthenticatedClient::default();
let repo_data = fetch_sparse_repodata(
channels.iter().map(AsRef::as_ref),
[Platform::current()],
&authenticated_client,
)
.await?;

// When package name filter contains * (wildcard), it will search and display a list of packages matching this filter
if package_name_filter.contains('*') {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use indicatif::HumanBytes;
use miette::IntoDiagnostic;

use rattler_digest::{compute_file_digest, Sha256};
use rattler_networking::AuthenticatedClient;

use tokio::fs::File;
use tokio_util::io::ReaderStream;

use crate::{default_authenticated_client, progress};
use crate::progress;

/// Upload a package to a prefix.dev channel
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -39,7 +41,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
HumanBytes(filesize)
);

let client = default_authenticated_client();
let client = AuthenticatedClient::default();

let sha256sum = format!(
"{:x}",
Expand Down
10 changes: 5 additions & 5 deletions src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
config, consts, default_authenticated_client, install, install_pypi, lock_file, prefix::Prefix,
progress, Project,
};
use crate::{config, consts, install, install_pypi, lock_file, prefix::Prefix, progress, Project};
use miette::IntoDiagnostic;

use crate::lock_file::lock_file_satisfies_project;
Expand All @@ -10,6 +7,7 @@ use crate::project::virtual_packages::verify_current_platform_has_required_virtu
use rattler::install::{PythonInfo, Transaction};
use rattler_conda_types::{Platform, PrefixRecord, RepoDataRecord};
use rattler_lock::CondaLock;
use rattler_networking::AuthenticatedClient;
use rattler_repodata_gateway::sparse::SparseRepoData;
use rip::index::PackageDb;
use rip::resolve::SDistResolution;
Expand Down Expand Up @@ -179,6 +177,7 @@ pub async fn get_up_to_date_prefix(
let python_status = if !no_install {
update_prefix_conda(
&prefix,
project.authenticated_client().clone(),
installed_packages_future.await.into_diagnostic()??,
&lock_file,
Platform::current(),
Expand Down Expand Up @@ -300,6 +299,7 @@ impl PythonStatus {
/// Updates the environment to contain the packages from the specified lock-file
pub async fn update_prefix_conda(
prefix: &Prefix,
authenticated_client: AuthenticatedClient,
installed_packages: Vec<PrefixRecord>,
lock_file: &CondaLock,
platform: Platform,
Expand All @@ -325,7 +325,7 @@ pub async fn update_prefix_conda(
&installed_packages,
prefix.root().to_path_buf(),
config::get_cache_dir()?,
default_authenticated_client(),
authenticated_client,
),
)
.await?;
Expand Down
16 changes: 0 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,11 @@ pub mod utils;
mod pypi_marker_env;
mod pypi_tags;

use once_cell::sync::Lazy;
pub use project::Project;
use rattler_networking::retry_policies::ExponentialBackoff;
use rattler_networking::AuthenticatedClient;
use reqwest::Client;

/// The default retry policy employed by pixi.
/// TODO: At some point we might want to make this configurable.
pub fn default_retry_policy() -> ExponentialBackoff {
ExponentialBackoff::builder().build_with_max_retries(3)
}

/// Returns the default client to use for networking.
pub fn default_client() -> Client {
static CLIENT: Lazy<Client> = Lazy::new(Default::default);
CLIENT.clone()
}

/// Returns the default authenticated client to use for rattler authenticated networking.
pub fn default_authenticated_client() -> AuthenticatedClient {
static CLIENT: Lazy<AuthenticatedClient> =
Lazy::new(|| AuthenticatedClient::from_client(default_client(), Default::default()));
CLIENT.clone()
}
26 changes: 5 additions & 21 deletions src/project/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,7 @@ mod tests {
.clone()
.into_iter()
.flat_map(|d| d.into_iter())
.map(|(name, spec)| format!(
"{} = {}",
name.as_source_str(),
Item::from(spec).to_string()
))
.map(|(name, spec)| format!("{} = {}", name.as_source_str(), Item::from(spec)))
.join("\n"));
}

Expand All @@ -1104,10 +1100,7 @@ mod tests {
// Initially the dependency should exist
assert!(manifest
.feature_mut(feature_name)
.expect(&*format!(
"feature `{}` should exist",
feature_name.as_str()
))
.expect(&format!("feature `{}` should exist", feature_name.as_str()))
.targets
.for_opt_target(platform.map(TargetSelector::Platform).as_ref())
.unwrap()
Expand All @@ -1130,10 +1123,7 @@ mod tests {
// The dependency should no longer exist
assert!(manifest
.feature_mut(feature_name)
.expect(&*format!(
"feature `{}` should exist",
feature_name.as_str()
))
.expect(&format!("feature `{}` should exist", feature_name.as_str()))
.targets
.for_opt_target(platform.map(TargetSelector::Platform).as_ref())
.unwrap()
Expand Down Expand Up @@ -1163,10 +1153,7 @@ mod tests {
// Initially the dependency should exist
assert!(manifest
.feature_mut(feature_name)
.expect(&*format!(
"feature `{}` should exist",
feature_name.as_str()
))
.expect(&format!("feature `{}` should exist", feature_name.as_str()))
.targets
.for_opt_target(platform.map(TargetSelector::Platform).as_ref())
.unwrap()
Expand All @@ -1184,10 +1171,7 @@ mod tests {
// The dependency should no longer exist
assert!(manifest
.feature_mut(feature_name)
.expect(&*format!(
"feature `{}` should exist",
feature_name.as_str()
))
.expect(&format!("feature `{}` should exist", feature_name.as_str()))
.targets
.for_opt_target(platform.map(TargetSelector::Platform).as_ref())
.unwrap()
Expand Down
26 changes: 24 additions & 2 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use once_cell::sync::OnceCell;
use rattler_conda_types::{
Channel, GenericVirtualPackage, MatchSpec, PackageName, Platform, Version,
};
use rattler_networking::AuthenticatedClient;
use rip::{index::PackageDb, normalize_index_url};
use std::hash::Hash;
use std::{
Expand All @@ -26,7 +27,6 @@ use std::{
use crate::{
config,
consts::{self, PROJECT_MANIFEST},
default_client,
task::Task,
};
use manifest::{EnvironmentName, Manifest, PyPiRequirement, SystemRequirements};
Expand Down Expand Up @@ -87,6 +87,10 @@ pub struct Project {
root: PathBuf,
/// The PyPI package db for this project
package_db: OnceCell<Arc<PackageDb>>,
/// Reqwest client shared for this project
client: reqwest::Client,
/// Authenticated reqwest client shared for this project
authenticated_client: AuthenticatedClient,
/// The manifest for the project
pub(crate) manifest: Manifest,
}
Expand All @@ -103,9 +107,14 @@ impl Debug for Project {
impl Project {
/// Constructs a new instance from an internal manifest representation
pub fn from_manifest(manifest: Manifest) -> Self {
let client = reqwest::Client::new();
let authenticated_client =
AuthenticatedClient::from_client(client.clone(), Default::default());
Self {
root: Default::default(),
package_db: Default::default(),
client,
authenticated_client,
manifest,
}
}
Expand Down Expand Up @@ -160,6 +169,8 @@ impl Project {
Ok(Self {
root: root.to_owned(),
package_db: Default::default(),
client: Default::default(),
authenticated_client: Default::default(),
manifest: manifest?,
})
}
Expand Down Expand Up @@ -338,7 +349,7 @@ impl Project {
.package_db
.get_or_try_init(|| {
PackageDb::new(
default_client(),
self.client().clone(),
&self.pypi_index_urls(),
&config::get_cache_dir()?.join("pypi/"),
)
Expand All @@ -347,6 +358,17 @@ impl Project {
})?
.as_ref())
}

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

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

/// Iterates over the current directory and all its parent directories and returns the first
Expand Down
Loading

0 comments on commit d5f2adb

Please sign in to comment.