Skip to content

Commit

Permalink
Optimize Client: Reduce size from 16 to 8 (#655)
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu authored Jan 6, 2023
1 parent ecb572d commit 305bf81
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/binstalk-downloader/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ pub struct HttpError {
err: reqwest::Error,
}

#[derive(Clone, Debug)]
pub struct Client {
#[derive(Debug)]
struct Inner {
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 {
/// * `per` - must not be 0.
/// * `num_request` - maximum number of requests to be processed for
Expand Down Expand Up @@ -76,22 +79,22 @@ impl Client {
.tcp_nodelay(false)
.build()?;

Ok(Client {
Ok(Client(Arc::new(Inner {
client: client.clone(),
rate_limit: Arc::new(Mutex::new(
rate_limit: Mutex::new(
ServiceBuilder::new()
.rate_limit(num_request.get(), per)
.service(client),
)),
})
),
})))
}

inner(user_agent.as_ref(), min_tls, per, num_request)
}

/// Return inner reqwest client.
pub fn get_inner(&self) -> &reqwest::Client {
&self.client
&self.0.client
}

async fn send_request_inner(
Expand All @@ -110,7 +113,7 @@ impl Client {
// the future, then release the lock before
// polling the future, which performs network I/O that could
// 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?;

Expand Down

0 comments on commit 305bf81

Please sign in to comment.