Skip to content

Instantly share code, notes, and snippets.

@devsnek
Last active July 20, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devsnek/8eb906b790bf9b0e8879fba4fe155cd7 to your computer and use it in GitHub Desktop.
Save devsnek/8eb906b790bf9b0e8879fba4fe155cd7 to your computer and use it in GitHub Desktop.
class Ratelimiter {
constructor (options = { timeout: 1, limit: 5 }) {
this.tracked = {};
this.limit = options.limit;
setInterval(() => {
for (const x in this.tracked) {
if (this.tracked[x] > 0) this.tracked[x]--;
}
}, options.timeout * 1000);
}
check (id) {
if (!(id in this.tracked)) {
this.tracked[id] = 0;
}
this.tracked[id]++;
if (this.tracked[id] > this.limit) return this.tracked[id];
return true;
}
reset (id) {
this.tracked[id] = 0;
}
}
module.exports = Ratelimiter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment