Skip to content

Instantly share code, notes, and snippets.

@appellation
Created March 7, 2017 18:07
Show Gist options
  • Save appellation/a50f445ac46c669b60d4e82a8588a8e1 to your computer and use it in GitHub Desktop.
Save appellation/a50f445ac46c669b60d4e82a8588a8e1 to your computer and use it in GitHub Desktop.
class Ratelimiter {
constructor({ limit = 1, frequency = 5 }) {
this.count = 0;
this.limit = limit;
this.frequency = frequency * 1000;
this._timeout();
}
_timeout() {
if(this.count > 0) this.count--;
setTimeout(this._timeout.bind(this), this.frequency);
}
check(condition = true) {
if(!condition) return false;
return this.count++ > this.limit;
}
}
module.exports = Ratelimiter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment