mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 04:28:43 +00:00
Optimize Client
: Reduce size from 16 to 8 (#655)
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
ecb572d02d
commit
305bf8123d
1 changed files with 12 additions and 9 deletions
|
@ -41,12 +41,15 @@ pub struct HttpError {
|
||||||
err: reqwest::Error,
|
err: reqwest::Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Client {
|
struct Inner {
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
rate_limit: Arc<Mutex<RateLimit<reqwest::Client>>>,
|
rate_limit: Mutex<RateLimit<reqwest::Client>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Client(Arc<Inner>);
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// * `per` - must not be 0.
|
/// * `per` - must not be 0.
|
||||||
/// * `num_request` - maximum number of requests to be processed for
|
/// * `num_request` - maximum number of requests to be processed for
|
||||||
|
@ -76,14 +79,14 @@ impl Client {
|
||||||
.tcp_nodelay(false)
|
.tcp_nodelay(false)
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
Ok(Client {
|
Ok(Client(Arc::new(Inner {
|
||||||
client: client.clone(),
|
client: client.clone(),
|
||||||
rate_limit: Arc::new(Mutex::new(
|
rate_limit: Mutex::new(
|
||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
.rate_limit(num_request.get(), per)
|
.rate_limit(num_request.get(), per)
|
||||||
.service(client),
|
.service(client),
|
||||||
)),
|
),
|
||||||
})
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
inner(user_agent.as_ref(), min_tls, per, num_request)
|
inner(user_agent.as_ref(), min_tls, per, num_request)
|
||||||
|
@ -91,7 +94,7 @@ impl Client {
|
||||||
|
|
||||||
/// Return inner reqwest client.
|
/// Return inner reqwest client.
|
||||||
pub fn get_inner(&self) -> &reqwest::Client {
|
pub fn get_inner(&self) -> &reqwest::Client {
|
||||||
&self.client
|
&self.0.client
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_request_inner(
|
async fn send_request_inner(
|
||||||
|
@ -110,7 +113,7 @@ impl Client {
|
||||||
// the future, then release the lock before
|
// the future, then release the lock before
|
||||||
// polling the future, which performs network I/O that could
|
// polling the future, which performs network I/O that could
|
||||||
// take really long.
|
// take really long.
|
||||||
let future = self.rate_limit.lock().await.ready().await?.call(request);
|
let future = self.0.rate_limit.lock().await.ready().await?.call(request);
|
||||||
|
|
||||||
let response = future.await?;
|
let response = future.await?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue