From 55555b88dd44366e27d2c7cc02166995a3835a69 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Wed, 4 Jul 2018 13:52:33 -0700 Subject: Add http::ratelimiting::offset Add a function to access a copy of the ratelimiting module's internal `OFFSET` static binding. --- src/http/ratelimiting.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'src/http') diff --git a/src/http/ratelimiting.rs b/src/http/ratelimiting.rs index e9008b4..b8152d1 100644 --- a/src/http/ratelimiting.rs +++ b/src/http/ratelimiting.rs @@ -56,20 +56,9 @@ use std::{ }; use super::{HttpError, LightMethod}; -/// The calculated offset of the time difference between Discord and the client -/// in seconds. -/// -/// This does not have millisecond precision as calculating that isn't -/// realistic. +/// Refer to [`offset`]. /// -/// This is used in ratelimiting to help determine how long to wait for -/// pre-emptive ratelimits. For example, if the client is 2 seconds ahead, then -/// the client would think the ratelimit is over 2 seconds before it actually is -/// and would then send off queued requests. Using an offset, we can know that -/// there's actually still 2 seconds left (+/- some milliseconds). -/// -/// This isn't a definitive solution to fix all problems, but it can help with -/// some precision gains. +/// [`offset`]: fn.offset.html static mut OFFSET: Option = None; lazy_static! { @@ -540,6 +529,27 @@ impl RateLimit { } } +/// The calculated offset of the time difference between Discord and the client +/// in seconds. +/// +/// This does not have millisecond precision as calculating that isn't +/// realistic. +/// +/// This is used in ratelimiting to help determine how long to wait for +/// pre-emptive ratelimits. For example, if the client is 2 seconds ahead, then +/// the client would think the ratelimit is over 2 seconds before it actually is +/// and would then send off queued requests. Using an offset, we can know that +/// there's actually still 2 seconds left (+/- some milliseconds). +/// +/// This isn't a definitive solution to fix all problems, but it can help with +/// some precision gains. +/// +/// This will return `None` if an HTTP request hasn't been made, meaning that +/// no offset could have been calculated. +pub fn offset() -> Option { + unsafe { OFFSET } +} + fn calculate_offset(header: Option<&[Vec]>) { // Get the current time as soon as possible. let now = Utc::now().timestamp(); -- cgit v1.2.3