Created
March 7, 2017 18:07
-
-
Save appellation/a50f445ac46c669b60d4e82a8588a8e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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