aboutsummaryrefslogtreecommitdiff
path: root/src/http/ratelimiting.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-10-10 20:08:11 -0700
committerZeyla Hellyer <[email protected]>2017-10-10 20:08:11 -0700
commit93e0a4215c915b98cf433ac6d0bcfbc60f0168ec (patch)
tree727111506d1f89cd8a511b8b79c102131222421f /src/http/ratelimiting.rs
parentResume on resumable session invalidations (diff)
downloadserenity-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/ratelimiting.rs')
-rw-r--r--src/http/ratelimiting.rs16
1 files changed, 7 insertions, 9 deletions
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")? {