From 55ccaca57051b3fbd47cf7fa288014d9c36f6952 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Sun, 5 Feb 2017 08:42:38 -0800 Subject: Make GLOBAL ratelimit mutex a unit Instead of making the GLOBAL ratelimit arc-mutex a RateLimit, make it an empty unit. --- src/client/rest/mod.rs | 2 +- src/client/rest/ratelimiting.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'src/client') diff --git a/src/client/rest/mod.rs b/src/client/rest/mod.rs index 639b901..b905921 100644 --- a/src/client/rest/mod.rs +++ b/src/client/rest/mod.rs @@ -24,7 +24,7 @@ //! [`Context`]: ../struct.Context.html //! [model]: ../../model/index.html -mod ratelimiting; +pub mod ratelimiting; use hyper::client::{ Client as HyperClient, diff --git a/src/client/rest/ratelimiting.rs b/src/client/rest/ratelimiting.rs index e915fb7..e0f78c9 100644 --- a/src/client/rest/ratelimiting.rs +++ b/src/client/rest/ratelimiting.rs @@ -50,7 +50,7 @@ use time; use ::internal::prelude::*; lazy_static! { - static ref GLOBAL: Arc> = Arc::new(Mutex::new(RateLimit::default())); + static ref GLOBAL: Arc> = Arc::new(Mutex::new(())); static ref ROUTES: Arc>> = Arc::new(Mutex::new(HashMap::default())); } @@ -147,8 +147,16 @@ pub fn perform<'a, F>(route: Route, f: F) -> Result // 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") -- cgit v1.2.3