rate limiter tweaks, first message deleted asap

This commit is contained in:
Erik 2021-06-12 17:35:03 +03:00
parent e77f3c6254
commit b3257ae0d0
No known key found for this signature in database
GPG Key ID: 7E862371D3409F16

View File

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