Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxy): remove system proxy cache #309

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ socks = ["dep:tokio-socks"]
native-roots = ["dep:rustls-native-certs"]
webpki-roots = ["dep:webpki-root-certs"]

# Optional disable internal proxy cache
internal_proxy_sys_no_cache = []

# Optional enable impersonate from str
impersonate_str = []

Expand Down
26 changes: 12 additions & 14 deletions src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,9 @@ impl Client {
}),
}
}
}

impl Client {
/// Get the client user agent
#[inline]
pub fn user_agent(&self) -> Option<&HeaderValue> {
Expand Down Expand Up @@ -1633,7 +1635,7 @@ impl Client {
Ok(self)
}

/// private mut ref to inner
/// Private mut ref to inner
#[inline(always)]
fn inner_mut(&mut self) -> &mut ClientRef {
Arc::make_mut(&mut self.inner)
Expand Down Expand Up @@ -1796,22 +1798,14 @@ pin_project! {
url: Url,
headers: HeaderMap,
body: Option<Option<Bytes>>,

version: Option<Version>,

urls: Vec<Url>,

retry_count: usize,
max_retry_count: usize,

redirect: Option<redirect::Policy>,

cookie_store: CookieStoreOption,

network_scheme: NetworkScheme,

client: Arc<ClientRef>,

#[pin]
in_flight: ResponseFuture,
#[pin]
Expand Down Expand Up @@ -1900,8 +1894,6 @@ impl PendingRequest {
}

fn is_retryable_error(err: &(dyn std::error::Error + 'static)) -> bool {
use hyper2::h2;

// pop the legacy::Error
let err = if let Some(err) = err.source() {
err
Expand All @@ -1910,15 +1902,20 @@ fn is_retryable_error(err: &(dyn std::error::Error + 'static)) -> bool {
};

if let Some(cause) = err.source() {
if let Some(err) = cause.downcast_ref::<h2::Error>() {
if let Some(err) = cause.downcast_ref::<hyper2::h2::Error>() {
// They sent us a graceful shutdown, try with a new connection!
if err.is_go_away() && err.is_remote() && err.reason() == Some(h2::Reason::NO_ERROR) {
if err.is_go_away()
&& err.is_remote()
&& err.reason() == Some(hyper2::h2::Reason::NO_ERROR)
{
return true;
}

// REFUSED_STREAM was sent from the server, which is safe to retry.
// https://www.rfc-editor.org/rfc/rfc9113.html#section-8.7-3.2
if err.is_reset() && err.is_remote() && err.reason() == Some(h2::Reason::REFUSED_STREAM)
if err.is_reset()
&& err.is_remote()
&& err.reason() == Some(hyper2::h2::Reason::REFUSED_STREAM)
{
return true;
}
Expand Down Expand Up @@ -2111,6 +2108,7 @@ impl Future for PendingRequest {
return Poll::Ready(Err(error::url_bad_uri(self.url.clone())));
}
};

let body = match self.body {
Some(Some(ref body)) => Body::reusable(body.clone()),
_ => Body::empty(),
Expand Down
15 changes: 4 additions & 11 deletions src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
#[cfg(feature = "socks")]
use std::net::SocketAddr;
use std::sync::{Arc, LazyLock};
use std::sync::Arc;

use crate::into_url::{IntoUrl, IntoUrlSealed};
use crate::Url;
Expand Down Expand Up @@ -277,16 +277,9 @@ impl Proxy {
}

pub(crate) fn system() -> Proxy {
static SYS_PROXIES: LazyLock<Arc<SystemProxyMap>> =
LazyLock::new(|| Arc::new(get_sys_proxies(get_from_platform())));

let mut proxy = if cfg!(feature = "internal_proxy_sys_no_cache") {
Proxy::new(Intercept::System(Arc::new(get_sys_proxies(
get_from_platform(),
))))
} else {
Proxy::new(Intercept::System(SYS_PROXIES.clone()))
};
let mut proxy = Proxy::new(Intercept::System(Arc::new(get_sys_proxies(
get_from_platform(),
))));
proxy.no_proxy = NoProxy::from_env();
proxy
}
Expand Down
Loading