diff options
author | marzavec <admin@marzavec.com> | 2019-04-08 02:04:10 +0200 |
---|---|---|
committer | marzavec <admin@marzavec.com> | 2019-04-08 02:04:10 +0200 |
commit | f353ecbd9e9e8e409303bb4f5c5590e95f579869 (patch) | |
tree | 3345967ae59a46f59fe86c0d6fda432115594927 /server/src/serverLib/RateLimiter.js | |
parent | refactoring 1 of 2 (diff) | |
download | hackchat-f353ecbd9e9e8e409303bb4f5c5590e95f579869.tar.gz hackchat-f353ecbd9e9e8e409303bb4f5c5590e95f579869.zip |
refactoring 2 of 2
Diffstat (limited to '')
-rw-r--r-- | server/src/serverLib/RateLimiter.js | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/server/src/serverLib/RateLimiter.js b/server/src/serverLib/RateLimiter.js index 87a1f3a..43cf077 100644 --- a/server/src/serverLib/RateLimiter.js +++ b/server/src/serverLib/RateLimiter.js @@ -13,25 +13,24 @@ class RateLimiter { * Create a ratelimiter instance. */ constructor () { - this._records = {}; - this._halflife = 30 * 1000; // milliseconds - this._threshold = 25; - this._hashes = []; + this.records = {}; + this.halflife = 30 * 1000; // milliseconds + this.threshold = 25; + this.hashes = []; } /** * Finds current score by `id` * * @param {String} id target id / address - * @public * - * @memberof Police + * @return {Object} Object containing the record meta */ search (id) { - let record = this._records[id]; + let record = this.records[id]; if (!record) { - record = this._records[id] = { + record = this.records[id] = { time: Date.now(), score: 0 } @@ -45,9 +44,8 @@ class RateLimiter { * * @param {String} id target id / address * @param {Number} deltaScore amount to adjust current score by - * @public * - * @memberof Police + * @return {Boolean} True if record threshold has been exceeded */ frisk (id, deltaScore) { let record = this.search(id); @@ -56,11 +54,11 @@ class RateLimiter { return true; } - record.score *= Math.pow(2, -(Date.now() - record.time ) / this._halflife); + record.score *= Math.pow(2, -(Date.now() - record.time ) / this.halflife); record.score += deltaScore; record.time = Date.now(); - if (record.score >= this._threshold) { + if (record.score >= this.threshold) { return true; } @@ -71,28 +69,22 @@ class RateLimiter { * Statically set server to no longer accept traffic from `id` * * @param {String} id target id / address - * @public - * - * @memberof Police */ arrest (id, hash) { let record = this.search(id); record.arrested = true; - this._hashes[hash] = id; + this.hashes[hash] = id; } /** * Remove statically assigned limit from `id` * * @param {String} id target id / address - * @public - * - * @memberof Police */ pardon (id) { - if (typeof this._hashes[id] !== 'undefined') { - id = this._hashes[id]; + if (typeof this.hashes[id] !== 'undefined') { + id = this.hashes[id]; } let record = this.search(id); |