From 0c7a8168428489614e3541bb923fa33c00fc6624 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 4 Apr 2022 10:24:45 -0700 Subject: [PATCH] fix(rate-limits): Adding artifical delay to anon requests --- package.json | 6 +++--- src/middleware/route-ratelimit.js | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 28991b6..c36395f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/middleware/route-ratelimit.js b/src/middleware/route-ratelimit.js index ee2f20c..8415917 100644 --- a/src/middleware/route-ratelimit.js +++ b/src/middleware/route-ratelimit.js @@ -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) {