Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gateway): remove ratelimiter sleep from new #2232

Merged
merged 3 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions twilight-gateway/src/ratelimiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ 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));

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

Self {
delay,
Expand Down Expand Up @@ -180,7 +180,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 +199,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 +226,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 +240,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