fix(rate-limits): Adding artifical delay to anon requests

This commit is contained in:
Chris Troutner
2022-04-04 10:24:45 -07:00
parent 6603a004c4
commit 0c7a816842
2 changed files with 12 additions and 3 deletions
+3 -3
View File
@@ -18,9 +18,9 @@
"lint": "standard --env mocha --fix",
"test-v4": "export NETWORK=mainnet && nyc --reporter=text mocha --exit --timeout 60000 test/v4/",
"test-v5": "export NETWORK=mainnet && nyc --reporter=text mocha --exit --timeout 60000 test/v5/",
"test:integration": "mocha test/v4/integration",
"test:integration:slpdb": "mocha --timeout 25000 -g '#validate2Single' test/v4/integration/slp*.js",
"test:integration:nft": "mocha --timeout 25000 -g '#nft' test/v4/integration/nft.js",
"test:integration": "mocha test/v5/integration",
"test:integration:slpdb": "mocha --timeout 25000 -g '#validate2Single' test/v5/integration/slp*.js",
"test:integration:nft": "mocha --timeout 25000 -g '#nft' test/v5/integration/nft.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/",
"docs": "./node_modules/.bin/apidoc -i src/routes/v5 -o docs",
+9
View File
@@ -213,6 +213,10 @@ class RateLimits {
next()
}
sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
// A wrapper for Redis-based rate limiter.
// Will return false if the user has not exceeded the rate limit. Otherwise
// it will return the 'res' object with an error status and message, which
@@ -267,6 +271,11 @@ class RateLimits {
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
// Add artificial delay to slow down freeloaders.
if (pointsToConsume === ANON_LIMITS) {
await this.sleep(500)
}
// Signal that the user has not exceeded their rate limits.
return false
} catch (err) {