From 9958821371f5a81c979140ef29d8029bd0ffe2b9 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 4 Apr 2022 10:51:28 -0700 Subject: [PATCH] fix(rate-limits): Adding delay to 422 response for anons too --- src/middleware/route-ratelimit.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/middleware/route-ratelimit.js b/src/middleware/route-ratelimit.js index 8415917..e9ddecf 100644 --- a/src/middleware/route-ratelimit.js +++ b/src/middleware/route-ratelimit.js @@ -33,6 +33,10 @@ const { RateLimiterRedis } = require('rate-limiter-flexible') const wlogger = require('../util/winston-logging') const config = require('../../config') +// The amount of delay in milliseconds to add to anonymous requests, in order +// to slow down freeloaders that are hitting the system too hard. +const ANON_DELAY = 1000 + let _this // Global pointer to instance of class, when 'this' context is lost. // Setup Redis to track rate limits for each user. @@ -273,7 +277,7 @@ class RateLimits { // Add artificial delay to slow down freeloaders. if (pointsToConsume === ANON_LIMITS) { - await this.sleep(500) + await this.sleep(ANON_DELAY) } // Signal that the user has not exceeded their rate limits. @@ -299,6 +303,11 @@ class RateLimits { } wlogger.info(logData) + // Add artificial delay to slow down freeloaders. + if (pointsToConsume === ANON_LIMITS) { + await this.sleep(ANON_DELAY) + } + // Rate limited was triggered res.status(429) // https://github.com/Bitcoin-com/rest.bitcoin.com/issues/330 return res.json({