Skip to content

Commit

Permalink
remove uncessary syscall by reusing instant
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgotf committed Jul 2, 2023
1 parent 7f5b7bf commit 93bf33c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 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 @@ -40,10 +40,11 @@ impl CommandRatelimiter {
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.
delay.as_mut().reset(Instant::now());
delay.as_mut().reset(now);

Self {
delay,
Expand Down

0 comments on commit 93bf33c

Please sign in to comment.