Skip to content

Commit 008501a

Browse files
committed
refactor: reuse calls to try_old_curl macro
At least doc duplicate can be eliminiated
1 parent cdd9649 commit 008501a

File tree

3 files changed

+43
-49
lines changed

3 files changed

+43
-49
lines changed

src/cargo/core/package.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use std::time::{Duration, Instant};
1010

1111
use anyhow::Context;
1212
use bytesize::ByteSize;
13-
use curl::easy::{Easy, HttpVersion};
13+
use curl::easy::Easy;
1414
use curl::multi::{EasyHandle, Multi};
1515
use lazycell::LazyCell;
16-
use log::{debug, warn};
16+
use log::debug;
1717
use semver::Version;
1818
use serde::Serialize;
1919

@@ -725,32 +725,8 @@ impl<'a, 'cfg> Downloads<'a, 'cfg> {
725725
handle.http_headers(headers)?;
726726
}
727727

728-
// Enable HTTP/2 to be used as it'll allow true multiplexing which makes
729-
// downloads much faster.
730-
//
731-
// Currently Cargo requests the `http2` feature of the `curl` crate
732-
// which means it should always be built in. On OSX, however, we ship
733-
// cargo still linked against the system libcurl. Building curl with
734-
// ALPN support for HTTP/2 requires newer versions of OSX (the
735-
// SecureTransport API) than we want to ship Cargo for. By linking Cargo
736-
// against the system libcurl then older curl installations won't use
737-
// HTTP/2 but newer ones will. All that to basically say we ignore
738-
// errors here on OSX, but consider this a fatal error to not activate
739-
// HTTP/2 on all other platforms.
740-
if self.set.multiplexing {
741-
crate::try_old_curl!(handle.http_version(HttpVersion::V2), "HTTP2");
742-
} else {
743-
handle.http_version(HttpVersion::V11)?;
744-
}
745-
746-
// This is an option to `libcurl` which indicates that if there's a
747-
// bunch of parallel requests to the same host they all wait until the
748-
// pipelining status of the host is known. This means that we won't
749-
// initiate dozens of connections to crates.io, but rather only one.
750-
// Once the main one is opened we realized that pipelining is possible
751-
// and multiplexing is possible with static.crates.io. All in all this
752-
// reduces the number of connections down to a more manageable state.
753-
crate::try_old_curl!(handle.pipewait(true), "pipewait");
728+
// Enable HTTP/2 if possible.
729+
crate::try_old_curl_http2_pipewait!(self.set.multiplexing, handle);
754730

755731
handle.write_function(move |buf| {
756732
debug!("{} - {} bytes of data", token, buf.len());

src/cargo/sources/registry/http_remote.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Access to a HTTP-based crate registry. See [`HttpRegistry`] for details.
22
33
use crate::core::{PackageId, SourceId};
4-
use crate::ops::{self};
4+
use crate::ops;
55
use crate::sources::registry::download;
66
use crate::sources::registry::MaybeLock;
77
use crate::sources::registry::{LoadResponse, RegistryConfig, RegistryData};
@@ -11,9 +11,9 @@ use crate::util::network::sleep::SleepTracker;
1111
use crate::util::{auth, Config, Filesystem, IntoUrl, Progress, ProgressStyle};
1212
use anyhow::Context;
1313
use cargo_util::paths;
14-
use curl::easy::{Easy, HttpVersion, List};
14+
use curl::easy::{Easy, List};
1515
use curl::multi::{EasyHandle, Multi};
16-
use log::{debug, trace, warn};
16+
use log::{debug, trace};
1717
use std::cell::RefCell;
1818
use std::collections::{HashMap, HashSet};
1919
use std::fs::{self, File};
@@ -618,20 +618,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
618618
handle.follow_location(true)?;
619619

620620
// Enable HTTP/2 if possible.
621-
if self.multiplexing {
622-
crate::try_old_curl!(handle.http_version(HttpVersion::V2), "HTTP2");
623-
} else {
624-
handle.http_version(HttpVersion::V11)?;
625-
}
626-
627-
// This is an option to `libcurl` which indicates that if there's a
628-
// bunch of parallel requests to the same host they all wait until the
629-
// pipelining status of the host is known. This means that we won't
630-
// initiate dozens of connections to crates.io, but rather only one.
631-
// Once the main one is opened we realized that pipelining is possible
632-
// and multiplexing is possible with static.crates.io. All in all this
633-
// reduces the number of connections done to a more manageable state.
634-
crate::try_old_curl!(handle.pipewait(true), "pipewait");
621+
crate::try_old_curl_http2_pipewait!(self.multiplexing, handle);
635622

636623
let mut headers = List::new();
637624
// Include a header to identify the protocol. This allows the server to

src/cargo/util/network/mod.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,51 @@ impl<T> PollExt<T> for Poll<T> {
2020
}
2121
}
2222

23-
// When dynamically linked against libcurl, we want to ignore some failures
24-
// when using old versions that don't support certain features.
23+
/// When dynamically linked against libcurl, we want to ignore some failures
24+
/// when using old versions that don't support certain features.
2525
#[macro_export]
2626
macro_rules! try_old_curl {
2727
($e:expr, $msg:expr) => {
2828
let result = $e;
2929
if cfg!(target_os = "macos") {
3030
if let Err(e) = result {
31-
warn!("ignoring libcurl {} error: {}", $msg, e);
31+
::log::warn!("ignoring libcurl {} error: {}", $msg, e);
3232
}
3333
} else {
34+
use ::anyhow::Context;
3435
result.with_context(|| {
35-
anyhow::format_err!("failed to enable {}, is curl not built right?", $msg)
36+
::anyhow::format_err!("failed to enable {}, is curl not built right?", $msg)
3637
})?;
3738
}
3839
};
3940
}
41+
42+
/// Enable HTTP/2 and pipewait to be used as it'll allow true multiplexing
43+
/// which makes downloads much faster.
44+
///
45+
/// Currently Cargo requests the `http2` feature of the `curl` crate which
46+
/// means it should always be built in. On OSX, however, we ship cargo still
47+
/// linked against the system libcurl. Building curl with ALPN support for
48+
/// HTTP/2 requires newer versions of OSX (the SecureTransport API) than we
49+
/// want to ship Cargo for. By linking Cargo against the system libcurl then
50+
/// older curl installations won't use HTTP/2 but newer ones will. All that to
51+
/// basically say we ignore errors here on OSX, but consider this a fatal error
52+
/// to not activate HTTP/2 on all other platforms.
53+
///
54+
/// `pipewait` is an option which indicates that if there's a bunch of parallel
55+
/// requests to the same host they all wait until the pipelining status of the
56+
/// host is known. This means that we won't initiate dozens of connections but
57+
/// rather only one. Once the main one is opened we realized that pipelining is
58+
/// possible and multiplexing is possible. All in all this reduces the number
59+
/// of connections down to a more manageable state.
60+
#[macro_export]
61+
macro_rules! try_old_curl_http2_pipewait {
62+
($multiplexing:expr, $handle:expr) => {
63+
if $multiplexing {
64+
$crate::try_old_curl!($handle.http_version(curl::easy::HttpVersion::V2), "HTTP/2");
65+
} else {
66+
$handle.http_version(curl::easy::HttpVersion::V11)?;
67+
}
68+
$crate::try_old_curl!($handle.pipewait(true), "pipewait");
69+
};
70+
}

0 commit comments

Comments
 (0)