fix(rate-limits): Adding delay to 422 response for anons too

This commit is contained in:
Chris Troutner
2022-04-04 10:51:28 -07:00
parent d5f7426a99
commit 9958821371
+10 -1
View File
@@ -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({