Skip to content

Commit

Permalink
Handle rate limit exceeded error
Browse files Browse the repository at this point in the history
Closes #71. Fixes #51.
  • Loading branch information
MaheraFurniturewala authored and ljharb committed Apr 26, 2022
1 parent 70cdd5b commit e5374c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bin/can-merge
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function outputStatus(response) {
}
});
}
console.error(`API Points: used - ${response.rateLimit.cost}\tremaining - ${response.rateLimit.remaining}`);
console.error(chalk.gray(`API Points: ${response.rateLimit.cost} used\t${response.rateLimit.remaining} remaining`));
}
const { repo, pr, sha } = args;

Expand Down
18 changes: 18 additions & 0 deletions utils/watch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
'use strict';

const chalk = require('chalk');
const evaluatePending = require('../utils/evaluatePending');

async function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

module.exports = async function watch(retryDelay, getResponse) {
const response = await getResponse();

const { remaining: remAPIPoints, cost: lastReqCost, resetAt } = response.rateLimit;
const currentTime = new Date();
const resetTime = new Date(resetAt);
const milliSecondsLeft = resetTime - currentTime;
const secondsLeft = Math.ceil(milliSecondsLeft / 1000);

if ((remAPIPoints < lastReqCost) && (retryDelay < milliSecondsLeft)) {
console.error(chalk.grey(`API points exhausted. Command will run again in ${secondsLeft} seconds...`));
await delay(milliSecondsLeft);
}

const isPendingChecks = evaluatePending(response);

if (!isPendingChecks) {
Expand Down

0 comments on commit e5374c1

Please sign in to comment.