diff options
Diffstat (limited to 'src/http')
| -rw-r--r-- | src/http/mod.rs | 34 | ||||
| -rw-r--r-- | src/http/ratelimiting.rs | 18 |
2 files changed, 33 insertions, 19 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index 099a33c..3fb5b7b 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -38,6 +38,7 @@ use hyper::net::HttpsConnector; use hyper::{header, Error as HyperError, Result as HyperResult, Url}; use hyper_native_tls::NativeTlsClient; use multipart::client::Multipart; +use parking_lot::Mutex; use self::ratelimiting::Route; use serde_json; use std::collections::BTreeMap; @@ -46,7 +47,7 @@ use std::fmt::Write as FmtWrite; use std::fs::File; use std::io::{ErrorKind as IoErrorKind, Read}; use std::path::{Path, PathBuf}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use constants; use internal::prelude::*; use model::*; @@ -98,7 +99,7 @@ lazy_static! { /// # fn main() { /// # try_main().unwrap(); /// # } -pub fn set_token(token: &str) { TOKEN.lock().unwrap().clone_from(&token.to_string()); } +pub fn set_token(token: &str) { TOKEN.lock().clone_from(&token.to_string()); } /// Adds a [`User`] as a recipient to a [`Group`]. /// @@ -625,7 +626,12 @@ pub fn delete_role(guild_id: u64, role_id: u64) -> Result<()> { pub fn delete_webhook(webhook_id: u64) -> Result<()> { verify( 204, - request!(Route::WebhooksId, delete, "/webhooks/{}", webhook_id), + request!( + Route::WebhooksId(webhook_id), + delete, + "/webhooks/{}", + webhook_id, + ), ) } @@ -788,7 +794,7 @@ pub fn edit_profile(map: &JsonMap) -> Result<CurrentUser> { let mut value = serde_json::from_reader::<HyperResponse, Value>(response)?; if let Some(map) = value.as_object_mut() { - if !TOKEN.lock().unwrap().starts_with("Bot ") { + if !TOKEN.lock().starts_with("Bot ") { if let Some(Value::String(token)) = map.remove("token") { set_token(&token); } @@ -873,7 +879,12 @@ pub fn edit_role_position(guild_id: u64, role_id: u64, position: u64) -> Result< // external crates being incredibly messy and misleading in the end user's view. pub fn edit_webhook(webhook_id: u64, map: &Value) -> Result<Webhook> { let body = map.to_string(); - let response = request!(Route::WebhooksId, patch(body), "/webhooks/{}", webhook_id); + let response = request!( + Route::WebhooksId(webhook_id), + patch(body), + "/webhooks/{}", + webhook_id, + ); serde_json::from_reader::<HyperResponse, Webhook>(response) .map_err(From::from) @@ -1536,7 +1547,12 @@ pub fn get_voice_regions() -> Result<Vec<VoiceRegion>> { /// /// [`get_webhook_with_token`]: fn.get_webhook_with_token.html pub fn get_webhook(webhook_id: u64) -> Result<Webhook> { - let response = request!(Route::WebhooksId, get, "/webhooks/{}", webhook_id); + let response = request!( + Route::WebhooksId(webhook_id), + get, + "/webhooks/{}", + webhook_id, + ); serde_json::from_reader::<HyperResponse, Webhook>(response) .map_err(From::from) @@ -1642,7 +1658,7 @@ pub fn send_files<'a, T, It: IntoIterator<Item=T>>(channel_id: u64, files: It, m let mut request = Request::with_connector(Method::Post, url, &connector)?; request .headers_mut() - .set(header::Authorization(TOKEN.lock().unwrap().clone())); + .set(header::Authorization(TOKEN.lock().clone())); request .headers_mut() .set(header::UserAgent(constants::USER_AGENT.to_string())); @@ -1650,7 +1666,7 @@ pub fn send_files<'a, T, It: IntoIterator<Item=T>>(channel_id: u64, files: It, m let mut request = Multipart::from_request(request)?; let mut file_num = "0".to_string(); - for file in files { + for file in files.into_iter() { match file.into() { AttachmentType::Bytes((mut bytes, filename)) => { request @@ -1801,7 +1817,7 @@ pub fn unpin_message(channel_id: u64, message_id: u64) -> Result<()> { fn request<'a, F>(route: Route, f: F) -> Result<HyperResponse> where F: Fn() -> RequestBuilder<'a> { let response = ratelimiting::perform(route, || { - f().header(header::Authorization(TOKEN.lock().unwrap().clone())) + f().header(header::Authorization(TOKEN.lock().clone())) .header(header::ContentType::json()) })?; diff --git a/src/http/ratelimiting.rs b/src/http/ratelimiting.rs index dbaca6b..08dc9ee 100644 --- a/src/http/ratelimiting.rs +++ b/src/http/ratelimiting.rs @@ -44,8 +44,9 @@ use chrono::Utc; use hyper::client::{RequestBuilder, Response}; use hyper::header::Headers; use hyper::status::StatusCode; +use parking_lot::Mutex; use std::collections::HashMap; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::time::Duration; use std::{str, thread, i64}; use super::{HttpError, LightMethod}; @@ -80,10 +81,8 @@ lazy_static! { /// ```rust,no_run /// use serenity::http::ratelimiting::{ROUTES, Route}; /// - /// let routes = ROUTES.lock().unwrap(); - /// - /// if let Some(route) = routes.get(&Route::ChannelsId(7)) { - /// println!("Reset time at: {}", route.lock().unwrap().reset); + /// if let Some(route) = ROUTES.lock().get(&Route::ChannelsId(7)) { + /// println!("Reset time at: {}", route.lock().reset); /// } /// ``` /// @@ -338,7 +337,7 @@ pub enum Route { /// Route for the `/voice/regions` path. VoiceRegions, /// Route for the `/webhooks/:webhook_id` path. - WebhooksId, + WebhooksId(u64), /// Route where no ratelimit headers are in place (i.e. user account-only /// routes). /// @@ -352,7 +351,7 @@ pub(crate) fn perform<'a, F>(route: Route, f: F) -> Result<Response> loop { // This will block if another thread already has the global // unlocked already (due to receiving an x-ratelimit-global). - let _ = GLOBAL.lock().expect("global route lock poisoned"); + let _ = GLOBAL.lock(); // Perform pre-checking here: // @@ -364,7 +363,6 @@ pub(crate) fn perform<'a, F>(route: Route, f: F) -> Result<Response> // - then, perform the request let bucket = Arc::clone(ROUTES .lock() - .expect("routes poisoned") .entry(route) .or_insert_with(|| { Arc::new(Mutex::new(RateLimit { @@ -374,7 +372,7 @@ pub(crate) fn perform<'a, F>(route: Route, f: F) -> Result<Response> })) })); - let mut lock = bucket.lock().unwrap(); + let mut lock = bucket.lock(); lock.pre_hook(&route); let response = super::retry(&f)?; @@ -396,7 +394,7 @@ pub(crate) fn perform<'a, F>(route: Route, f: F) -> Result<Response> return Ok(response); } else { let redo = if response.headers.get_raw("x-ratelimit-global").is_some() { - let _ = GLOBAL.lock().expect("global route lock poisoned"); + let _ = GLOBAL.lock(); Ok( if let Some(retry_after) = parse_header(&response.headers, "retry-after")? { |