From b3257ae0d0dbe38e4e2a4d709f1ac53e06ae8f85 Mon Sep 17 00:00:00 2001 From: Navy Date: Sat, 12 Jun 2021 17:35:03 +0300 Subject: [PATCH] rate limiter tweaks, first message deleted asap --- structure/client/RateLimiter.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/structure/client/RateLimiter.js b/structure/client/RateLimiter.js index ef3e011..77776c1 100644 --- a/structure/client/RateLimiter.js +++ b/structure/client/RateLimiter.js @@ -16,9 +16,10 @@ class RateLimiter { this.deleteTimeouts = {}; //same as above this.lastSend = {}; //used by limitSend + this.lastDelete = {}; this.sendInterval = 7.5; //How frequently sending is allowed in seconds - this.deleteInterval = 1.5; //How frequently delete queues should be executed + this.deleteInterval = 2.5; //How frequently delete queues should be executed } @@ -41,7 +42,8 @@ class RateLimiter { if(!this.deleteQueue[channel.id]) this.deleteQueue[channel.id] = []; this.deleteQueue[channel.id].push({ message, resolve, reject }); - if(!this.deleteTimeouts[channel.id] || this.deleteTimeouts[channel.id]._destroyed) this.deleteTimeouts[channel.id] = setTimeout(this.delete.bind(this), this.deleteInterval*1000, channel); + //if(!this.deleteTimeouts[channel.id] || this.deleteTimeouts[channel.id]._destroyed) this.deleteTimeouts[channel.id] = setTimeout(this.delete.bind(this), this.deleteInterval*1000, channel); + this.delete(channel); }); @@ -56,14 +58,23 @@ class RateLimiter { queue = [...this.deleteQueue[channel.id]], deleteThese = []; - for(const item of queue) { + const lastDelete = this.lastDelete[channel.id]; + const now = Math.floor(Date.now() / 1000); + if (now - lastDelete < this.deleteInterval) { + const timeout = this.deleteTimeouts[channel.id]; + if (!timeout || timeout._destroyed) this.deleteTimeouts[channel.id] = setTimeout(this.delete.bind(this), this.deleteInterval*1000, channel); + return; + } + this.lastDelete[channel.id] = now; + + for(const item of queue) { // Organise into arrays const { message, resolve, reject } = item; if(deleteThese.length <= 100) { deleteThese.push(message); resolves.push(resolve); rejects.push(reject); this.deleteQueue[channel.id].shift(); - } else { + } else { // left over messages go in next batch this.deleteTimeouts[channel.id] = setTimeout(this.delete.bind(this), this.deleteInterval*1000, channel); break; }