diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-10 20:08:11 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-10 20:08:11 -0700 |
| commit | 93e0a4215c915b98cf433ac6d0bcfbc60f0168ec (patch) | |
| tree | 727111506d1f89cd8a511b8b79c102131222421f /src/http | |
| parent | Resume on resumable session invalidations (diff) | |
| download | serenity-93e0a4215c915b98cf433ac6d0bcfbc60f0168ec.tar.xz serenity-93e0a4215c915b98cf433ac6d0bcfbc60f0168ec.zip | |
Switch to parking_lot::{Mutex, RwLock}
Switch to the `parking_lot` crate's implementations of
`std::sync::Mutex` and `std::sync::RwLock`, which are more efficient.
A writeup on why `parking_lot` is more efficient can be read here:
<https://github.com/Amanieu/parking_lot>
Upgrade path:
Modify `mutex.lock().unwrap()` usage to `mutex.lock()` (not needing to
unwrap or handle a result), and
`rwlock.read().unwrap()`/`rwlock.write().unwrap()` usage to
`rwlock.read()` and `rwlock.write()`.
For example, modify:
```rust
use serenity::CACHE;
println!("{}", CACHE.read().unwrap().user.id);
```
to:
```rust
use serenity::CACHE;
println!("{}", CACHE.read().user.id);
```
Diffstat (limited to 'src/http')
| -rw-r--r-- | src/http/mod.rs | 11 | ||||
| -rw-r--r-- | src/http/ratelimiting.rs | 16 |
2 files changed, 13 insertions, 14 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index 2348794..d6b32f1 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`]. /// @@ -793,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); } @@ -1639,7 +1640,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())); @@ -1798,7 +1799,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 93ca4f6..09c2527 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); /// } /// ``` /// @@ -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 = ROUTES .lock() - .expect("routes poisoned") .entry(route) .or_insert_with(|| { Arc::new(Mutex::new(RateLimit { @@ -375,7 +373,7 @@ pub(crate) fn perform<'a, F>(route: Route, f: F) -> Result<Response> }) .clone(); - let mut lock = bucket.lock().unwrap(); + let mut lock = bucket.lock(); lock.pre_hook(&route); let response = super::retry(&f)?; @@ -397,7 +395,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")? { |