From 12b0ded4a3c1ad0cb0463858f856c205535d7509 Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Sun, 26 Jun 2016 18:41:32 +0200 Subject: [PATCH] GitHub auth: use UTC epoch seconds instead of milliseconds --- lib/github-auth.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/github-auth.js b/lib/github-auth.js index dfcbea6..b7e4442 100644 --- a/lib/github-auth.js +++ b/lib/github-auth.js @@ -140,12 +140,16 @@ function rmReqRemaining(token) { reqReset.delete(token); } +function utcEpochSeconds() { + return ((Date.now() / 1000) >>> 0); +} + // Retrieve a user token if there is one for which we believe there are requests // remaining. Return undefined if we could not find one. function getReqRemainingToken() { // Go through the user tokens. // Keep the first one which is usable or has reset. - var now = +new Date(); + var now = utcEpochSeconds(); for (var token of reqReset.keys()) { var reqs = reqRemaining.get(token); var reset = reqReset.get(token); @@ -212,9 +216,10 @@ function githubRequest(request, url, query, cb) { rmGithubToken(githubToken); } else { var remaining = +res.headers['x-ratelimit-remaining']; + // reset is in UTC epoch seconds. var reset = +res.headers['x-ratelimit-reset']; console.log('GitHub auth: token', githubToken, 'rate limit', remaining, - 'reset', reset, 'now', +new Date()); + 'reset', reset, 'now', utcEpochSeconds()); setReqRemaining(githubToken, remaining, reset); } }