bulk pet loader rate limits itself

This commit is contained in:
Emi Matchu 2013-07-02 14:10:24 -07:00
parent abe35c6fd9
commit 4acc0e22c8
2 changed files with 24 additions and 8 deletions

View file

@ -115,7 +115,10 @@ var DEBUG = (document.location.search.substr(0, 6) == '?debug');
$(document.body).addClass('js'); $(document.body).addClass('js');
bulk_load_queue = new (function BulkLoadQueue() { bulk_load_queue = new (function BulkLoadQueue() {
var pets = [], url = form.attr('action') + '.json'; var RECENTLY_SENT_INTERVAL_IN_SECONDS = 60;
var RECENTLY_SENT_MAX = 10;
var pets = [], url = form.attr('action') + '.json',
recently_sent_count = 0, loading = false;
function Pet(name) { function Pet(name) {
var el = $('#bulk-pets-submission-template').tmpl({pet_name: name}). var el = $('#bulk-pets-submission-template').tmpl({pet_name: name}).
@ -124,12 +127,12 @@ var DEBUG = (document.location.search.substr(0, 6) == '?debug');
this.load = function () { this.load = function () {
el.removeClass('waiting').addClass('loading'); el.removeClass('waiting').addClass('loading');
var response_el = el.find('span.response'); var response_el = el.find('span.response');
pets.shift();
loading = true;
$.ajax({ $.ajax({
complete: function (data) { complete: function (data) {
pets.shift(); loading = false;
if(pets.length) { loadNextIfReady();
pets[0].load();
}
}, },
data: {name: name}, data: {name: name},
dataType: 'json', dataType: 'json',
@ -146,6 +149,12 @@ var DEBUG = (document.location.search.substr(0, 6) == '?debug');
type: 'post', type: 'post',
url: url url: url
}); });
recently_sent_count++;
setTimeout(function () {
recently_sent_count--;
loadNextIfReady();
}, RECENTLY_SENT_INTERVAL_IN_SECONDS * 1000);
} }
} }
@ -154,7 +163,13 @@ var DEBUG = (document.location.search.substr(0, 6) == '?debug');
if(name.length) { if(name.length) {
var pet = new Pet(name); var pet = new Pet(name);
pets.push(pet); pets.push(pet);
if(pets.length == 1) pet.load(); if(pets.length == 1) loadNextIfReady();
}
}
function loadNextIfReady() {
if(!loading && recently_sent_count < RECENTLY_SENT_MAX && pets.length) {
pets[0].load();
} }
} }
})(); })();

View file

@ -750,8 +750,9 @@ en:
header: Model pets in bulk header: Model pets in bulk
explanation: explanation:
Got a lot of pets to model? Just keep typing them into the box below, Got a lot of pets to model? Just keep typing them into the box below,
or even paste in a whole list of names, one name per line. Thanks for or even paste in a whole list of names, one name per line. We'll
your help! stagger them over time to keep our servers happy. Thanks for your
help!
submit: Load pet submit: Load pet
add: Add add: Add
clear: Clear clear: Clear