Skip to content

Commit

Permalink
Maintain a single, re-used HTTP client
Browse files Browse the repository at this point in the history
Instead of creating a new HTTP client for each request, keep a single one for
every HTTP request.

This will make things faster due to keepalives.
  • Loading branch information
Zeyla Hellyer committed Aug 4, 2018
1 parent 602c5a7 commit 8c0e5a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
40 changes: 17 additions & 23 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ use std::{
sync::Arc
};

lazy_static! {
static ref CLIENT: HyperClient = {
let tc = NativeTlsClient::new().expect("Unable to make http client");
let connector = HttpsConnector::new(tc);

HyperClient::with_connector(connector)
};
}

/// An method used for ratelimiting special routes.
///
/// This is needed because `hyper`'s `Method` enum does not derive Copy.
Expand Down Expand Up @@ -709,12 +718,10 @@ pub fn delete_webhook(webhook_id: u64) -> Result<()> {
///
/// [`Webhook`]: ../model/webhook/struct.Webhook.html
pub fn delete_webhook_with_token(webhook_id: u64, token: &str) -> Result<()> {
let client = request_client!();

verify(
204,
retry(|| {
client
CLIENT
.delete(&format!(api!("/webhooks/{}/{}"), webhook_id, token))
}).map_err(Error::Hyper)?,
)
Expand Down Expand Up @@ -989,10 +996,9 @@ pub fn edit_webhook(webhook_id: u64, map: &Value) -> Result<Webhook> {
/// [`edit_webhook`]: fn.edit_webhook.html
pub fn edit_webhook_with_token(webhook_id: u64, token: &str, map: &JsonMap) -> Result<Webhook> {
let body = serde_json::to_string(map)?;
let client = request_client!();

let response = retry(|| {
client
CLIENT
.patch(&format!(api!("/webhooks/{}/{}"), webhook_id, token))
.body(&body)
}).map_err(Error::Hyper)?;
Expand Down Expand Up @@ -1068,10 +1074,8 @@ pub fn execute_webhook(webhook_id: u64,
-> Result<Option<Message>> {
let body = serde_json::to_string(map)?;

let client = request_client!();

let response = retry(|| {
client
CLIENT
.post(&format!(
api!("/webhooks/{}/{}?wait={}"),
webhook_id,
Expand All @@ -1097,10 +1101,8 @@ pub fn execute_webhook(webhook_id: u64,
///
/// Does not require authentication.
pub fn get_active_maintenances() -> Result<Vec<Maintenance>> {
let client = request_client!();

let response = retry(|| {
client.get(status!("/scheduled-maintenances/active.json"))
CLIENT.get(status!("/scheduled-maintenances/active.json"))
})?;

let mut map: BTreeMap<String, Value> = serde_json::from_reader(response)?;
Expand Down Expand Up @@ -1531,9 +1533,8 @@ pub fn get_message(channel_id: u64, message_id: u64) -> Result<Message> {
/// Gets X messages from a channel.
pub fn get_messages(channel_id: u64, query: &str) -> Result<Vec<Message>> {
let url = format!(api!("/channels/{}/messages{}"), channel_id, query);
let client = request_client!();

let response = request(Route::ChannelsIdMessages(channel_id), || client.get(&url))?;
let response = request(Route::ChannelsIdMessages(channel_id), || CLIENT.get(&url))?;

serde_json::from_reader::<HyperResponse, Vec<Message>>(response)
.map_err(From::from)
Expand Down Expand Up @@ -1586,9 +1587,7 @@ pub fn get_reaction_users(channel_id: u64,
///
/// Does not require authentication.
pub fn get_unresolved_incidents() -> Result<Vec<Incident>> {
let client = request_client!();

let response = retry(|| client.get(status!("/incidents/unresolved.json")))?;
let response = retry(|| CLIENT.get(status!("/incidents/unresolved.json")))?;

let mut map: BTreeMap<String, Value> = serde_json::from_reader(response)?;

Expand All @@ -1603,10 +1602,8 @@ pub fn get_unresolved_incidents() -> Result<Vec<Incident>> {
///
/// Does not require authentication.
pub fn get_upcoming_maintenances() -> Result<Vec<Maintenance>> {
let client = request_client!();

let response = retry(|| {
client.get(status!("/scheduled-maintenances/upcoming.json"))
CLIENT.get(status!("/scheduled-maintenances/upcoming.json"))
})?;

let mut map: BTreeMap<String, Value> = serde_json::from_reader(response)?;
Expand Down Expand Up @@ -1689,11 +1686,8 @@ pub fn get_webhook(webhook_id: u64) -> Result<Webhook> {
/// .expect("Error getting webhook");
/// ```
pub fn get_webhook_with_token(webhook_id: u64, token: &str) -> Result<Webhook> {
let client = request_client!();

let response = retry(|| {
client
.get(&format!(api!("/webhooks/{}/{}"), webhook_id, token))
CLIENT.get(&format!(api!("/webhooks/{}/{}"), webhook_id, token))
}).map_err(Error::Hyper)?;

serde_json::from_reader::<HyperResponse, Webhook>(response)
Expand Down
16 changes: 4 additions & 12 deletions src/internal/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@
#[cfg(feature = "http")]
macro_rules! request {
($route:expr, $method:ident($body:expr), $url:expr, $($rest:tt)*) => {{
let client = request_client!();

request($route, || client
request($route, || CLIENT
.$method(&format!(api!($url), $($rest)*))
.body(&$body))?
}};
($route:expr, $method:ident($body:expr), $url:expr) => {{
let client = request_client!();

request($route, || client
request($route, || CLIENT
.$method(api!($url))
.body(&$body))?
}};
($route:expr, $method:ident, $url:expr, $($rest:tt)*) => {{
let client = request_client!();

request($route, || client
request($route, || CLIENT
.$method(&format!(api!($url), $($rest)*)))?
}};
($route:expr, $method:ident, $url:expr) => {{
let client = request_client!();

request($route, || client
request($route, || CLIENT
.$method(api!($url)))?
}};
}
Expand Down

0 comments on commit 8c0e5a3

Please sign in to comment.