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; }