Skip to content

Commit

Permalink
refactor(gateway): remove ratelimiter sleep from new (#2232)
Browse files Browse the repository at this point in the history
This timer registration hack is slightly less horrible than the last one.
  • Loading branch information
vilgotf authored Jul 2, 2023
1 parent 035036e commit 7a1888d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions twilight-gateway/src/ratelimiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
pin::Pin,
task::{Context, Poll},
};
use tokio::time::{sleep, Duration, Instant, Sleep};
use tokio::time::{sleep_until, Duration, Instant, Sleep};

/// Number of commands allowed in a [`PERIOD`].
const COMMANDS_PER_PERIOD: u8 = 120;
Expand All @@ -37,13 +37,14 @@ pub struct CommandRatelimiter {

impl CommandRatelimiter {
/// Create a new ratelimiter with some capacity reserved for heartbeating.
pub(crate) async fn new(heartbeat_interval: Duration) -> Self {
pub(crate) fn new(heartbeat_interval: Duration) -> Self {
let allotted = nonreserved_commands_per_reset(heartbeat_interval);

let mut delay = Box::pin(sleep(Duration::ZERO));
let now = Instant::now();
let mut delay = Box::pin(sleep_until(now));

// Hack to register the timer.
(&mut delay).await;
delay.as_mut().reset(now);

Self {
delay,
Expand Down Expand Up @@ -180,7 +181,7 @@ mod tests {

#[tokio::test(start_paused = true)]
async fn full_reset() {
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL).await;
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL);

assert_eq!(ratelimiter.available(), ratelimiter.max());
for _ in 0..ratelimiter.max() {
Expand All @@ -199,7 +200,7 @@ mod tests {

#[tokio::test(start_paused = true)]
async fn half_reset() {
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL).await;
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL);

assert_eq!(ratelimiter.available(), ratelimiter.max());
for _ in 0..ratelimiter.max() / 2 {
Expand All @@ -226,7 +227,7 @@ mod tests {

#[tokio::test(start_paused = true)]
async fn constant_capacity() {
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL).await;
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL);
let max = ratelimiter.max();

for _ in 0..max {
Expand All @@ -240,7 +241,7 @@ mod tests {

#[tokio::test(start_paused = true)]
async fn spurious_poll() {
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL).await;
let mut ratelimiter = CommandRatelimiter::new(HEARTBEAT_INTERVAL);

for _ in 0..ratelimiter.max() {
ratelimiter.acquire().await;
Expand Down
2 changes: 1 addition & 1 deletion twilight-gateway/src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ impl Shard {
tracing::debug!(?heartbeat_interval, ?jitter, "received hello");

if self.config().ratelimit_messages() {
self.ratelimiter = Some(CommandRatelimiter::new(heartbeat_interval).await);
self.ratelimiter = Some(CommandRatelimiter::new(heartbeat_interval));
}

let mut interval = time::interval_at(Instant::now() + jitter, heartbeat_interval);
Expand Down

0 comments on commit 7a1888d

Please sign in to comment.