Added rate limiting logging and an error page for hitting the rate limit on opening characters

This commit is contained in:
Thaum Rystra
2018-04-02 14:26:55 +02:00
parent 164ba78c81
commit 742b26b0de
6 changed files with 79 additions and 7 deletions

View File

@@ -33,7 +33,6 @@ var ifKeyValid = function(apiKey, response, callback){
response.writeHead(403, "API key is invalid");
response.end();
} else if (isRateLimited(apiKey)){
console.log(`Rate limit hit by API key ${apiKey}`);
response.writeHead(429, "Too many requests");
response.end();
} else {
@@ -53,5 +52,11 @@ var rateLimiter = new RateLimiter();
rateLimiter.addRule({apiKey: String}, 2, 10000);
var isRateLimited = function(apiKey){
return !rateLimiter.check({apiKey}).allowed;
const limited = !rateLimiter.check({apiKey}).allowed
if (limited) {
console.log(`Rate limit hit by API key ${apiKey}`);
return true;
} else {
return false;
}
};