Skip to content

Commit

Permalink
Decorating a retry-after header for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Feb 17, 2019
1 parent a15f004 commit 26c39a5
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,21 @@ function keymaster (req, res) {
}

function rate (req, res, next) {
if (req.server.config.rate.enabled === false || req.unprotect) {
const config = req.server.config.rate;

if (config.enabled === false || req.unprotect) {
next();
} else {
const results = req.server.rate(req, req.server.config.rate.override),
const results = req.server.rate(req, config.override),
good = results.shift();

each(rateHeaders, (i, idx) => res.header(i, results[idx]));
res.header('retry-after', config.reset);

if (good) {
next();
} else {
res.error(req.server.config.rate.status || 429);
res.error(config.status || 429);
}
}
}
Expand Down

0 comments on commit 26c39a5

Please sign in to comment.