Skip to content

Commit

Permalink
Add http::ratelimiting::offset
Browse files Browse the repository at this point in the history
Add a function to access a copy of the ratelimiting module's internal `OFFSET`
static binding.
  • Loading branch information
Zeyla Hellyer committed Jul 4, 2018
1 parent ccd2506 commit 55555b8
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/http/ratelimiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64> = None;

lazy_static! {
Expand Down Expand Up @@ -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<i64> {
unsafe { OFFSET }
}

fn calculate_offset(header: Option<&[Vec<u8>]>) {
// Get the current time as soon as possible.
let now = Utc::now().timestamp();
Expand Down

0 comments on commit 55555b8

Please sign in to comment.