Skip to content

Commit

Permalink
Remove usage of Arc for reqwest::Client (#1705)
Browse files Browse the repository at this point in the history
`reqwest::Client` already uses `Arc` internally, we don't have to do it again.
  • Loading branch information
vaporoxx authored and arqunis committed Mar 15, 2022
1 parent 6476ed1 commit a1cdd7f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
23 changes: 11 additions & 12 deletions src/http/client.rs
Expand Up @@ -54,7 +54,7 @@ use crate::utils;
/// # }
/// ```
pub struct HttpBuilder {
client: Option<Arc<Client>>,
client: Option<Client>,
ratelimiter: Option<Ratelimiter>,
ratelimiter_disabled: Option<bool>,
token: Option<String>,
Expand Down Expand Up @@ -105,7 +105,7 @@ impl HttpBuilder {

/// Sets the [`reqwest::Client`]. If one isn't provided, a default one will
/// be used.
pub fn client(mut self, client: Arc<Client>) -> Self {
pub fn client(mut self, client: Client) -> Self {
self.client = Some(client);

self
Expand Down Expand Up @@ -168,11 +168,11 @@ impl HttpBuilder {

let client = self.client.unwrap_or_else(|| {
let builder = configure_client_backend(Client::builder());
Arc::new(builder.build().expect("Cannot build reqwest::Client"))
builder.build().expect("Cannot build reqwest::Client")
});

let ratelimiter = self.ratelimiter.unwrap_or_else(|| {
let client = Arc::clone(&client);
let client = client.clone();
Ratelimiter::new(client, token.to_string())
});

Expand Down Expand Up @@ -206,7 +206,7 @@ fn reason_into_header(reason: &str) -> Headers {
/// [`Error::Http`]: crate::error::Error::Http
/// [`Error::Json`]: crate::error::Error::Json
pub struct Http {
pub(crate) client: Arc<Client>,
pub(crate) client: Client,
pub ratelimiter: Ratelimiter,
pub ratelimiter_disabled: bool,
pub proxy: Option<Url>,
Expand All @@ -227,8 +227,8 @@ impl fmt::Debug for Http {
}

impl Http {
pub fn new(client: Arc<Client>, token: &str) -> Self {
let client2 = Arc::clone(&client);
pub fn new(client: Client, token: &str) -> Self {
let client2 = client.clone();

Http {
client,
Expand All @@ -246,7 +246,7 @@ impl Http {
let builder = configure_client_backend(Client::builder());
let built = builder.build().expect("Cannot build reqwest::Client");

let mut data = Self::new(Arc::new(built), "");
let mut data = Self::new(built, "");

data.application_id = application_id;

Expand All @@ -264,7 +264,7 @@ impl Http {
format!("Bot {}", token)
};

Self::new(Arc::new(built), &token)
Self::new(built, &token)
}

#[cfg(feature = "unstable_discord_api")]
Expand Down Expand Up @@ -3822,9 +3822,8 @@ impl AsRef<Http> for Http {

impl Default for Http {
fn default() -> Self {
let built = Client::builder().build().expect("Cannot build Reqwest::Client.");
let client = Arc::new(built);
let client2 = Arc::clone(&client);
let client = Client::builder().build().expect("Cannot build Reqwest::Client.");
let client2 = client.clone();

Self {
client,
Expand Down
6 changes: 3 additions & 3 deletions src/http/ratelimiting.rs
Expand Up @@ -83,7 +83,7 @@ use crate::internal::prelude::*;
/// [`remaining`]: Ratelimit::remaining
/// [`reset`]: Ratelimit::reset
pub struct Ratelimiter {
client: Arc<Client>,
client: Client,
global: Arc<Mutex<()>>,
// When futures is implemented, make tasks clear out their respective entry
// when the 'reset' passes.
Expand All @@ -107,11 +107,11 @@ impl Ratelimiter {
///
/// The bot token must be prefixed with `"Bot "`. The ratelimiter does not
/// prefix it.
pub fn new(client: Arc<Client>, token: impl Into<String>) -> Self {
pub fn new(client: Client, token: impl Into<String>) -> Self {
Self::_new(client, token.into())
}

fn _new(client: Arc<Client>, token: String) -> Self {
fn _new(client: Client, token: String) -> Self {
Self {
client,
global: Default::default(),
Expand Down

0 comments on commit a1cdd7f

Please sign in to comment.