Keep client in thread-local storage

This commit is contained in:
asonix 2023-06-23 15:01:56 -05:00
parent e005adfcf8
commit 74f35faa22

View file

@ -162,15 +162,25 @@ impl std::fmt::Debug for Requests {
} }
} }
pub(crate) fn build_client(user_agent: &str, pool_size: usize) -> Client { thread_local! {
let connector = Connector::new().limit(pool_size); static CLIENT: std::cell::OnceCell<Client> = std::cell::OnceCell::new();
}
Client::builder() pub(crate) fn build_client(user_agent: &str, pool_size: usize) -> Client {
.connector(connector) CLIENT.with(|client| {
.wrap(Tracing) client
.add_default_header(("User-Agent", user_agent.to_string())) .get_or_init(|| {
.timeout(Duration::from_secs(15)) let connector = Connector::new().limit(pool_size);
.finish()
Client::builder()
.connector(connector)
.wrap(Tracing)
.add_default_header(("User-Agent", user_agent.to_string()))
.timeout(Duration::from_secs(15))
.finish()
})
.clone()
})
} }
impl Requests { impl Requests {