Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
Switched rate limit retrieval to HEAD rather than GET
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEnigmaBlade authored and agentlame committed Jul 24, 2015
1 parent 69b734f commit 7a9dff6
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions tbutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,16 @@ function initwrapper() {
});
};

TBUtils.getHead = function (url, doneCallback) {
$.ajax({
type: 'HEAD',
url: url
})
.done(function (data, status, jqxhr) {
// data isn't needed; just the tip
doneCallback(status, jqxhr);
});
};

// Because normal .sort() is case sensitive.
TBUtils.saneSort = function (arr) {
Expand Down Expand Up @@ -1070,46 +1080,47 @@ function initwrapper() {

function getRatelimit() {
//return doChunk();
$.getJSON('/r/toolbox/wiki/ratelimit.json').done(function (data, status, jqxhr) {
var $body = $('body'),
ratelimitRemaining = jqxhr.getResponseHeader('x-ratelimit-remaining'),
ratelimitReset = jqxhr.getResponseHeader('x-ratelimit-reset');
$.log('ratelimitRemaining: ' + ratelimitRemaining + ' ratelimitReset: ' + (ratelimitReset / 60), false, SHORTNAME);

if (!$body.find('#ratelimit-counter').length) {
$('div[role="main"].content').append('<span id="ratelimit-counter"></span>');
}
TBUtils.getHead('/r/toolbox/wiki/ratelimit.json',
function (status, jqxhr) {
var $body = $('body'),
ratelimitRemaining = jqxhr.getResponseHeader('x-ratelimit-remaining'),
ratelimitReset = jqxhr.getResponseHeader('x-ratelimit-reset');
$.log('ratelimitRemaining: ' + ratelimitRemaining + ' ratelimitReset: ' + (ratelimitReset / 60), false, SHORTNAME);

if (!$body.find('#ratelimit-counter').length) {
$('div[role="main"].content').append('<span id="ratelimit-counter"></span>');
}

if (chunkSize + limit > parseInt(ratelimitRemaining)) {
$body.find('#ratelimit-counter').show();
var count = parseInt(ratelimitReset),
counter = 0;

function timer() {
count = count - 1;
if (count <= 0) {
clearInterval(counter);
$body.find('#ratelimit-counter').empty();
$body.find('#ratelimit-counter').hide();
doChunk();
return;
if (chunkSize + limit > parseInt(ratelimitRemaining)) {
$body.find('#ratelimit-counter').show();
var count = parseInt(ratelimitReset),
counter = 0;

function timer() {
count = count - 1;
if (count <= 0) {
clearInterval(counter);
$body.find('#ratelimit-counter').empty();
$body.find('#ratelimit-counter').hide();
doChunk();
return;
}

var minutes = Math.floor(count / 60);
var seconds = count - minutes * 60;

$body.find('#ratelimit-counter').html('<b>Oh dear, it seems we have hit a limit, waiting for ' + minutes + ' minutes and ' + seconds + ' seconds before resuming operations.</b>\
<br><br>\
<span class="rate-limit-explain"><b>tl;dr</b> <br> Reddit\'s current ratelimit allows for <i>' + ratelimitRemaining + ' requests</i>. We are currently trying to process <i>' + parseInt(chunkSize) + ' items</i>. Together with toolbox requests in the background that is cutting it a little bit too close. Luckily for us reddit tells us when the ratelimit will be reset, that is the timer you see now.</span>\
');
}

var minutes = Math.floor(count / 60);
var seconds = count - minutes * 60;
counter = setInterval(timer, 1000);

$body.find('#ratelimit-counter').html('<b>Oh dear, it seems we have hit a limit, waiting for ' + minutes + ' minutes and ' + seconds + ' seconds before resuming operations.</b>\
<br><br>\
<span class="rate-limit-explain"><b>tl;dr</b> <br> Reddit\'s current ratelimit allows for <i>' + ratelimitRemaining + ' requests</i>. We are currently trying to process <i>' + parseInt(chunkSize) + ' items</i>. Together with toolbox requests in the background that is cutting it a little bit too close. Luckily for us reddit tells us when the ratelimit will be reset, that is the timer you see now.</span>\
');
} else {
doChunk();
}

counter = setInterval(timer, 1000);

} else {
doChunk();
}
});
});
}

getRatelimit();
Expand Down

0 comments on commit 7a9dff6

Please sign in to comment.