Skip to content

Commit

Permalink
feat(proxy): remove system proxy cache (#309)
Browse files Browse the repository at this point in the history
* Remove system proxy cache

* fmt
  • Loading branch information
0x676e67 authored Jan 11, 2025
1 parent a043290 commit 7992c93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
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

0 comments on commit 7992c93

Please sign in to comment.