Skip to content

Commit

Permalink
PharmaLedger-IMI/epi-workspace#1042 added readiness probe
Browse files Browse the repository at this point in the history
  • Loading branch information
skutner committed Nov 16, 2022
1 parent 31c3177 commit d29f848
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
31 changes: 2 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ process.on('uncaughtException', err => {
const httpWrapper = require('./libs/http-wrapper');
const Server = httpWrapper.Server;

const TokenBucket = require('./libs/TokenBucket');
const START_TOKENS = 6000000;
const CHECK_FOR_RESTART_COMMAND_FILE_INTERVAL = 500;

(function loadDefaultComponents(){
Expand Down Expand Up @@ -41,8 +39,6 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart
}

let port = listeningPort || 8080;
const tokenBucket = new TokenBucket(START_TOKENS, 1, 10);

const conf = require('./config').getConfig();
const server = new Server(sslConfig);
server.config = conf;
Expand Down Expand Up @@ -127,31 +123,6 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart
next();
});

if (conf.preventRateLimit !== true) {
server.use(function (req, res, next) {
const ip = res.socket.remoteAddress;
tokenBucket.takeToken(ip, tokenBucket.COST_MEDIUM, function (err, remainedTokens) {
res.setHeader('X-RateLimit-Limit', tokenBucket.getLimitByCost(tokenBucket.COST_MEDIUM));
res.setHeader('X-RateLimit-Remaining', tokenBucket.getRemainingTokenByCost(remainedTokens, tokenBucket.COST_MEDIUM));

if (err) {
if (err === TokenBucket.ERROR_LIMIT_EXCEEDED) {
res.statusCode = 429;
} else {
res.statusCode = 500;
}

res.end();
return;
}

next();
});
});
} else {
logger.trace(`Rate limit mechanism disabled!`);
}

server.options('/*', function (req, res) {
const headers = {};
// IE8 does not allow domains to be specified, just the *
Expand All @@ -176,6 +147,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart
function addRootMiddlewares() {
const LoggerMiddleware = require('./middlewares/logger');
const AuthorisationMiddleware = require('./middlewares/authorisation');
const Throttler = require('./middlewares/throttler');
const OAuth = require('./middlewares/oauth');
const ResponseHeaderMiddleware = require('./middlewares/responseHeader');
const genericErrorMiddleware = require('./middlewares/genericErrorMiddleware');
Expand All @@ -187,6 +159,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart

genericErrorMiddleware(server);
requestEnhancements(server);
Throttler(server);

if(conf.enableJWTAuthorisation) {
new AuthorisationMiddleware(server);
Expand Down
55 changes: 55 additions & 0 deletions middlewares/throttler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const TokenBucket = require("../../libs/TokenBucket");

function Throttler(server){
const logger = $$.getLogger("apihub", "Throttler");
const START_TOKENS = 6000000;
const tokenBucket = new TokenBucket(START_TOKENS, 1, 10);
let remainingTokens = START_TOKENS;
const conf = require('../../config').getConfig();
function throttlerHandler(req, res, next) {
const ip = res.socket.remoteAddress;
tokenBucket.takeToken(ip, tokenBucket.COST_MEDIUM, function (err, remainedTokens) {
res.setHeader('X-RateLimit-Limit', tokenBucket.getLimitByCost(tokenBucket.COST_MEDIUM));
res.setHeader('X-RateLimit-Remaining', tokenBucket.getRemainingTokenByCost(remainedTokens, tokenBucket.COST_MEDIUM));
remainingTokens = remainedTokens;
if (err) {
if (err === TokenBucket.ERROR_LIMIT_EXCEEDED) {
res.statusCode = 429;
} else {
res.statusCode = 500;
}

res.end();
return;
}

next();
});
}

function readyProbeHandler(req, res) {
const stats = {
remainingTokens: tokenBucket.getRemainingTokenByCost(remainingTokens, tokenBucket.COST_MEDIUM),
tokenLimit: tokenBucket.getLimitByCost(tokenBucket.COST_MEDIUM)
}

res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
res.write(JSON.stringify(stats));
res.end();
}

if (conf.preventRateLimit !== true) {
server.use(throttlerHandler);
server.get("/ready-probe", readyProbeHandler);
} else {
logger.trace(`Rate limit mechanism disabled!`);
server.get("/ready-probe", function (req, res) {
res.statusCode = 200;
res.write("Server ready");
res.end();
});
}
}

module.exports = Throttler;

0 comments on commit d29f848

Please # to comment.