reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Should I cache the reqwest client? It seems that if I cache the reqwest client, its proxies configuration will be continuously cached

Open Beace opened this issue 7 months ago • 3 comments

Looking through the code of reqwest, I saw that when using the Client:: builder() API, the system proxies will be obtained once.

If I keep reusing the same client, even if the system proxy configuration is updated, reqwest will still use the old proxy. My code is similar to this

fn create_reqwest_client() -> Client {
    let builder = Client::builder();
    builder
        .tcp_keepalive(Some(Duration::from_secs(10)))
        .pool_idle_timeout(Some(Duration::from_secs(10)))
        .build()
        .expect("create http client failed")
}

lazy_static::lazy_static! {
    static ref HTTP_CLIENT: Client = create_reqwest_client();
}

But if I initialize a reqwest client every time I make a request, and I'm worried there might be additional overhead. Is there a recommended solution?

Beace avatar May 12 '25 10:05 Beace

I believe this can be addressed by configure proxy per-request: https://github.com/seanmonstar/reqwest/issues/2641

Xuanwo avatar May 12 '25 10:05 Xuanwo

Or another option is to use the Proxy::custom(), and check the environment yourself.

seanmonstar avatar May 12 '25 10:05 seanmonstar

Or another option is to use the Proxy::custom(), and check the environment yourself.

Can we change the Proxy::system API to pub's ? Because I want to reuse reqwest's proxy implementation and just want to retrieve the system proxy again.

At the same time, I can also see that when errors are caused by the proxy, I can perceive that the user is using the proxy

Beace avatar May 12 '25 10:05 Beace