Skip to content

Commit 5072ba5

Browse files
committed
refactor(network): use async for registry network operations
1 parent 0c7001f commit 5072ba5

56 files changed

Lines changed: 2189 additions & 2184 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ curl = "0.4.49"
4545
# Do not upgrade curl-sys past 0.4.83
4646
# https://github.com/rust-lang/cargo/issues/16357
4747
curl-sys = "=0.4.83"
48+
dynosaur = "0.3"
4849
filetime = "0.2.27"
4950
flate2 = { version = "1.1.9", default-features = false, features = ["zlib-rs"] }
51+
futures = { version = "0.3", default-features = false, features = ["std", "executor"]}
52+
futures-timer = "3.0.3"
5053
git2 = "0.20.4"
5154
git2-curl = "0.21.0"
5255
# When updating this, also see if `gix-transport` further down needs updating or some auth-related tests will fail.
@@ -58,6 +61,7 @@ heck = "0.5.0"
5861
hex = "0.4.3"
5962
hmac = "0.12.1"
6063
home = "0.5.12"
64+
http = "1.4.0"
6165
http-auth = { version = "0.1.10", default-features = false }
6266
ignore = "0.4.25"
6367
im-rc = "15.1.0"
@@ -172,8 +176,10 @@ color-print.workspace = true
172176
crates-io.workspace = true
173177
curl = { workspace = true, features = ["http2"] }
174178
curl-sys.workspace = true
179+
dynosaur.workspace = true
175180
filetime.workspace = true
176181
flate2.workspace = true
182+
futures.workspace = true
177183
git2.workspace = true
178184
git2-curl.workspace = true
179185
gix.workspace = true
@@ -182,6 +188,7 @@ heck.workspace = true
182188
hex.workspace = true
183189
hmac.workspace = true
184190
home.workspace = true
191+
http.workspace = true
185192
http-auth.workspace = true
186193
ignore.workspace = true
187194
im-rc.workspace = true
@@ -225,6 +232,7 @@ unicode-ident.workspace = true
225232
url.workspace = true
226233
walkdir.workspace = true
227234
winnow.workspace = true
235+
futures-timer.workspace = true
228236

229237
[target.'cfg(target_has_atomic = "64")'.dependencies]
230238
tracing-chrome.workspace = true

crates/cargo-test-support/src/registry.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,6 @@ impl HttpServer {
823823
url,
824824
body,
825825
};
826-
println!("req: {:#?}", req);
827826
let response = self.route(&req);
828827
let buf = buf.get_mut();
829828
write!(buf, "HTTP/1.1 {}\r\n", response.code).unwrap();
@@ -1029,7 +1028,7 @@ impl HttpServer {
10291028
Response {
10301029
code: 401,
10311030
headers: vec![
1032-
r#"WWW-Authenticate: Cargo login_url="https://test-registry-login/me""#.to_string(),
1031+
r#"www-authenticate: Cargo login_url="https://test-registry-login/me""#.to_string(),
10331032
],
10341033
body: b"Unauthorized message from server.".to_vec(),
10351034
}

crates/resolver-tests/src/lib.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
pub mod helpers;
88
pub mod sat;
99

10+
use std::cell::RefCell;
1011
use std::cmp::{max, min};
1112
use std::collections::{BTreeMap, HashSet};
1213
use std::fmt;
13-
use std::task::Poll;
1414
use std::time::Instant;
1515

1616
use cargo::core::Resolve;
1717
use cargo::core::ResolveVersion;
1818
use cargo::core::SourceId;
1919
use cargo::core::dependency::DepKind;
20+
use cargo::core::registry::DynRegistry;
2021
use cargo::core::resolver::{self, ResolveOpts, VersionOrdering, VersionPreferences};
2122
use cargo::core::{Dependency, PackageId, Registry, Summary};
2223
use cargo::sources::IndexSummary;
@@ -131,15 +132,16 @@ pub fn resolve_with_global_context_raw(
131132
) -> CargoResult<Resolve> {
132133
struct MyRegistry<'a> {
133134
list: &'a [Summary],
134-
used: HashSet<PackageId>,
135+
used: RefCell<HashSet<PackageId>>,
135136
}
137+
136138
impl<'a> Registry for MyRegistry<'a> {
137-
fn query(
138-
&mut self,
139+
async fn query(
140+
&self,
139141
dep: &Dependency,
140142
kind: QueryKind,
141143
f: &mut dyn FnMut(IndexSummary),
142-
) -> Poll<CargoResult<()>> {
144+
) -> CargoResult<()> {
143145
for summary in self.list.iter() {
144146
let matched = match kind {
145147
QueryKind::Exact => dep.matches(summary),
@@ -148,11 +150,11 @@ pub fn resolve_with_global_context_raw(
148150
QueryKind::Normalized => true,
149151
};
150152
if matched {
151-
self.used.insert(summary.package_id());
153+
self.used.borrow_mut().insert(summary.package_id());
152154
f(IndexSummary::Candidate(summary.clone()));
153155
}
154156
}
155-
Poll::Ready(Ok(()))
157+
Ok(())
156158
}
157159

158160
fn describe_source(&self, _src: SourceId) -> String {
@@ -162,22 +164,18 @@ pub fn resolve_with_global_context_raw(
162164
fn is_replaced(&self, _src: SourceId) -> bool {
163165
false
164166
}
165-
166-
fn block_until_ready(&mut self) -> CargoResult<()> {
167-
Ok(())
168-
}
169167
}
170168
impl<'a> Drop for MyRegistry<'a> {
171169
fn drop(&mut self) {
172-
if std::thread::panicking() && self.list.len() != self.used.len() {
170+
if std::thread::panicking() && self.list.len() != self.used.get_mut().len() {
173171
// we found a case that causes a panic and did not use all of the input.
174172
// lets print the part of the input that was used for minimization.
175173
eprintln!(
176174
"Part used before drop: {:?}",
177175
PrettyPrintRegistry(
178176
self.list
179177
.iter()
180-
.filter(|s| { self.used.contains(&s.package_id()) })
178+
.filter(|s| { self.used.get_mut().contains(&s.package_id()) })
181179
.cloned()
182180
.collect()
183181
)
@@ -187,7 +185,7 @@ pub fn resolve_with_global_context_raw(
187185
}
188186
let mut registry = MyRegistry {
189187
list: registry,
190-
used: HashSet::new(),
188+
used: RefCell::new(HashSet::new()),
191189
};
192190

193191
let root_summary =
@@ -204,7 +202,7 @@ pub fn resolve_with_global_context_raw(
204202
let resolve = resolver::resolve(
205203
&[(root_summary, opts)],
206204
&[],
207-
&mut registry,
205+
DynRegistry::from_mut(&mut registry),
208206
&version_prefs,
209207
ResolveVersion::with_rust_version(None),
210208
Some(gctx),

crates/xtask-bump-check/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ git2.workspace = true
1313
semver.workspace = true
1414
tracing-subscriber.workspace = true
1515
tracing.workspace = true
16+
futures.workspace = true
1617

1718
[lints]
1819
workspace = true

crates/xtask-bump-check/src/xtask.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use std::collections::HashMap;
1616
use std::fmt::Write;
1717
use std::fs;
18-
use std::task;
1918

2019
use cargo::CargoResult;
2120
use cargo::core::Package;
@@ -433,15 +432,9 @@ fn check_crates_io<'a>(
433432
let current = member.version();
434433
let version_req = format!(">={current}");
435434
let query = Dependency::parse(*name, Some(&version_req), source_id)?;
436-
let possibilities = loop {
437-
// Exact to avoid returning all for path/git
438-
match registry.query_vec(&query, QueryKind::Exact) {
439-
task::Poll::Ready(res) => {
440-
break res?;
441-
}
442-
task::Poll::Pending => registry.block_until_ready()?,
443-
}
444-
};
435+
// Exact to avoid returning all for path/git
436+
let possibilities =
437+
futures::executor::block_on(registry.query_vec(&query, QueryKind::Exact))?;
445438
if possibilities.is_empty() {
446439
tracing::trace!("dep `{name}` has no version greater than or equal to `{current}`");
447440
} else {

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ use tracing::{debug, info};
397397

398398
use crate::core::Package;
399399
use crate::core::compiler::unit_graph::UnitDep;
400+
use crate::sources::source::Source as _;
400401
use crate::util;
401402
use crate::util::errors::CargoResult;
402403
use crate::util::interning::InternedString;

0 commit comments

Comments
 (0)