Skip to content

Commit

Permalink
Make GLOBAL ratelimit mutex a unit
Browse files Browse the repository at this point in the history
Instead of making the GLOBAL ratelimit arc-mutex a RateLimit, make it an
empty unit.
  • Loading branch information
Austin Hellyer committed Feb 5, 2017
1 parent df9a9a9 commit 55ccaca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/client/rest/mod.rs
Expand Up @@ -24,7 +24,7 @@
//! [`Context`]: ../struct.Context.html
//! [model]: ../../model/index.html

mod ratelimiting;
pub mod ratelimiting;

use hyper::client::{
Client as HyperClient,
Expand Down
14 changes: 11 additions & 3 deletions src/client/rest/ratelimiting.rs
Expand Up @@ -50,7 +50,7 @@ use time;
use ::internal::prelude::*;

lazy_static! {
static ref GLOBAL: Arc<Mutex<RateLimit>> = Arc::new(Mutex::new(RateLimit::default()));
static ref GLOBAL: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
static ref ROUTES: Arc<Mutex<HashMap<Route, RateLimit>>> = Arc::new(Mutex::new(HashMap::default()));
}

Expand Down Expand Up @@ -147,8 +147,16 @@ pub fn perform<'a, F>(route: Route, f: F) -> Result<Response>
// header. If the limit was 5 and is now 7, add 2 to the 'remaining'
if route != Route::None {
let redo = if response.headers.get_raw("x-ratelimit-global").is_some() {
let mut global = GLOBAL.lock().expect("global route lock poisoned");
global.post_hook(&response)
let _ = GLOBAL.lock().expect("global route lock poisoned");

Ok(if let Some(retry_after) = get_header(&response.headers, "retry-after")? {
debug!("Ratelimited: {:?}ms", retry_after);
thread::sleep(Duration::from_millis(retry_after as u64));

true
} else {
false
})
} else {
ROUTES.lock()
.expect("routes poisoned")
Expand Down

0 comments on commit 55ccaca

Please sign in to comment.